Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf - boolean operators

How can I use boolean operators like and or or using Thymeleaf?

For instance, if I want to show the data from a table if only one of the conditions is true.

<tr th:if="firstCondition or secondCondition">
  <td th:text="${entity.attr1}"</td>
  <td th:text="${entity.attr2}">Default Value</td>
</tr>
like image 841
Vinicin Avatar asked Aug 02 '13 00:08

Vinicin


2 Answers

Boolean operators work just like that. You use 'or', 'and' instead of the normal java nomenclature. You can also shorten your ifs.

You can try this:

<tr th:if="${violation.remainingDebt != 0 or violation.validity}">

You need to nest them up in the same curly brackets, independently if they are isolated considering the logical 'or' operation being tested.

Be wary though! This will only show you the tr and it's child elements if the if passes as true.

like image 90
Nimchip Avatar answered Oct 10 '22 23:10

Nimchip


Instead of using conditional operators && and || in the expression like we use in Java and Javascript, in Thymeleaf we use the text AND and OR for comparison.

OR condition example:

<div th:if="${fruit.name} == Apple OR ${fruit.name} == Orange ">
     <!-- fruit's name is either Apple or Orange -->
</div>

AND condition example:

<div th:if="${user.role} == 'ADMIN' AND ${user.property} == 'SPECIAL' ">
     <!-- User is admin and has SPECIAL previleges -->
</div>
like image 37
Lucky Avatar answered Oct 10 '22 22:10

Lucky