In some situations, you want a certain snippet of the Thymeleaf Template to appear in the result if a certain condition is evaluated as true. To do this you can use the attribute th:if. Note: In Thymeleaf, A variable or an expression is evaluated as false if its value is null, false, 0, "false", "off", "no".
Simple Conditionals: "if" and "unless" Thymeleaf provides th:if and th:unless conditional attributes to exclude elements based on a provided condition in the rendered document. Thymeleaf engine will evaluate the condition th:if="${user. active}" defined on the tr HTML element.
You can compare values and expressions using the >, <, >= and <= Comparators. These operators behave the same way as they would behave in most of the programming languages.
Thymeleaf th:switch, th:case Attributes Example with Spring boot. In this example, we will demonstrate the usage of th:switch and th:case attributes. If the User role is "Admin" then "User is an administrator" text prints on the web page. The th:case = "*" is the default case of the th:swith/th:case structure.
Thymeleaf has an equivalent to <c:choose>
and <c:when>
: the th:switch
and th:case
attributes introduced in Thymeleaf 2.0.
They work as you'd expect, using *
for the default case:
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>
See this for a quick explanation of syntax (or the Thymeleaf tutorials).
Disclaimer: As required by StackOverflow rules, I'm the author of Thymeleaf.
I tried this code to find out if a customer is logged in or anonymous. I did using the th:if
and th:unless
conditional expressions. Pretty simple way to do it.
<!-- IF CUSTOMER IS ANONYMOUS -->
<div th:if="${customer.anonymous}">
<div>Welcome, Guest</div>
</div>
<!-- ELSE -->
<div th:unless="${customer.anonymous}">
<div th:text=" 'Hi,' + ${customer.name}">Hi, User</div>
</div>
I'd like to share my example related to security in addition to Daniel Fernández.
<div th:switch="${#authentication}? ${#authorization.expression('isAuthenticated()')} : ${false}">
<span th:case="${false}">User is not logged in</span>
<span th:case="${true}">Logged in user</span>
<span th:case="*">Should never happen, but who knows...</span>
</div>
Here is complex expression with mixed 'authentication' and 'authorization' utility objects which produces 'true/false' result for thymeleaf template code.
The 'authentication' and 'authorization' utility objects came from thymeleaf extras springsecurity3 library. When 'authentication' object is not available OR authorization.expression('isAuthenticated()') evaluates to 'false', expression returns ${false}, otherwise ${true}.
You can use
If-then-else: (if) ? (then) : (else)
Example:
'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
It could be useful for the new people asking the same question.
Another solution - you can use local variable:
<div th:with="expr_result = ${potentially_complex_expression}">
<div th:if="${expr_result}">
<h2>Hello!</h2>
</div>
<div th:unless="${expr_result}">
<span class="xxx">Something else</span>
</div>
</div>
More about local variables:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#local-variables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With