Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change value of a variable in thymeleaf?

Tags:

html

thymeleaf

I am new to thymeleaf. I am bit confused now. Please check out the codes below

  <th:block th:with="${someVarible=false}">
    <th:block th:each="dem : ${demo}">
       <th:block th:if="${dem.status==0}">
             //Here I need to change the value of someVarible to true
       </th:block>
    </th:block>
    <th:block th:if="${someVariable}">Its true</th:block>
  </th:block>

I need to edit the value of someVarible . How can I do it. Thanks in advance.

like image 578
Akhil Sudhakaran Avatar asked Jan 15 '16 03:01

Akhil Sudhakaran


People also ask

How do you access variables in Thymeleaf?

In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: ${attributeName} , where attributeName in our case is messages .

How do you use Thymeleaf th value?

th:name => This would be the name of the value that you will be either passing to another page (Exemplar scenario). th:value => This would be the actual value that you would be passing.

How do you add text to Thymeleaf?

The Thymeleaf th:text tag will replace all the text in your h1 tag, that is the reason your output only shows "TITLE". You should place the <small> tags outside your h1 tag.

How do I print a value in Thymeleaf?

You can use the "th:text=#{variable}" to print a variable in the Thymeleaf.


2 Answers

You can't achieve your desired functionality as you described with Thymeleaf.

th:with does just local variable definition which is only available for evaluation inside that fragment.

<div class="example1" th:with="foo=${bar}">
    <!--/* foo is availabile here */-->
    <th:block th:text="${foo}" />
</div>
<div class="example2">
    <!--/* foo is NOT availabile here! */-->
</div>

And you can't change that variable in template. Thymeleaf is just presentation layer, you're trying to achieve something which has to be done on application layer (Java code).

On application layer (Java code) you could do:

Map<Integer, Boolean> fooMap = new HashMap<Integer, Boolean>();

for(Demo demo : demos) {
    if(demo.getStatus() == 0) {
        fooMap.put(demo.getId(), true);
    } else {
        fooMap.put(demo.getId(), false);
    }
}

And then on presentation layer (Thymeleaf):

<th:block th:each="demo : ${demos}">
    <th:block th:text="${demo.getId()}" />
</th:block>
<th:block th:each="demo : ${demos}">
    <th:block th:if="${fooMap.get(demo.getId()) == true}">It's true</th:block>
</th:block>

If you don't want to use HashMap you could use inheritance and extend object Demo.

(Please note that code which I wrote is not tested, so it may need some small fixed, but I hope that I helped you.)

like image 138
Lukas Grygar Avatar answered Oct 07 '22 11:10

Lukas Grygar


As Lukas said, it's not possible to change the value of a variable in Thymeleaf, because that only applies to the content within that element. However it is possibe to achieve something very similar using Thymeleaf only.

You can use Collection Selection and the ^[...] syntax to select the first element in your list that matches the criteria status==0. This expression would look like:

${demo.^[status==0]}

If the demo list contains an element with status==0, then that will be returned. Otherwise, it will result in null. This can be used directly in your th:if:

<th:block th:if="${demo.^[status==0]}">Its true</th:block>

Or, if you need to use someVariable for other things too, you can assign it to a variable using th:with (Docs):

<th:block th:with="someVariable=${demo.^[status==0]}">
    <th:block th:if="${someVariable}">Its true</th:block>
</th:block>
like image 44
Andrew Avatar answered Oct 07 '22 11:10

Andrew