Operators und Comperators
Bedingungen
<tr th:class="${row.even}? 'even' : 'odd'">
...
</tr>
Bedingungen können auch verschachtelt werden (mit Klammern). Wenn keine Else-Bedingung angegeben wird, wird einfach null zurückgegeben.
<tr th:class="${row.even}? 'alt'">
...
</tr>
Der Elvis-Operator
Der Elvis-Operator ist eigentlich dazu da, default-werte für variabeln zu Verfügung zu stellen, wenn eine Variable nicht vorhanden ist. Siehe Beispiel:
<span th:text="*{firstName}?: (*{admin}? 'Admin' : #{default.username})">Sebastian</span>
IF & Unless
<a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">view</a>
it will evaluate the specified expression as true following these rules:
- If value is not null:
- If value is a boolean and is true .
- If value is a number and is non-zero
- If value is a character and is non-zero
- If value is a String and is not “false”, “off” or “no”
- If value is not a boolean, a number, a character or a String.
- (If value is null, th:if will evaluate to false).
Also, th:if has a negative counterpart, th:unless:
<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>
Switch
Wenn bei einem Switch ein case "true" ist, dann werden dadurch alle anderen "false".
<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>