How to Do If-Else in Thymeleaf

How to do if-else in Thymeleaf?

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.

If/else in thymeleaf

From the documentation:

Thymeleaf's main goal is to bring elegant natural templates to your development workflow — HTML that can be correctly displayed in browsers and also work as static prototypes, allowing for stronger collaboration in development teams.

So this HTML like syntax is on purpose and there is no "simpler" one.

See also How to do if-else in Thymeleaf? for some alternatives.

How to use thymeleaf conditions - if - elseif - else

You conditional operator contains 3 results. It should have 2 like this.

condition ? first_expression : second_expression;

In your situation. I assume linea.estado is a boolean value

<td style="white-space: nowrap">
<span th:class="${linea.estado} ? 'label label-success' : 'label label-danger'"
th:text="${linea.estado}? #{label.glineas.estado.iniciado} : #{label.glineas.estado.finalizado}">
</span>
</td>

If you want 3 values to be output and given that the linea.estado is a string which may contain 'WARN', 'DANGER', 'INFO' then you can do something like this.

<span th:class="'label label-' + ((${linea.estado} == 'SUCCESS') ? 'success' : (${linea.estado} == 'DANGER') ? 'danger' : 'warning')"                   
th:text="...">
</span>

But the cleaner solution will be something like this

<span th:if="${linea.estado} == 'SUCCESS'" class="label label-success" th:text="#{label.glineas.estado.iniciado}"></span>
<span th:if="${linea.estado} == 'DANGER'" class="label label-danger" th:text="#{label.glineas.estado.finalizado}"></span>
<span th:if="${linea.estado} == 'WARN'" class="label label-warning" th:text="#{label.glineas.estado.configurado}"></span>

Or using Switch as mentioned by Patrick LC

  • be aware of syntax errors, as I didnt test any codes on runtime

If-Else in a th:each statement in Thymeleaf

<div  th:if="${currentSkills != null}">
<table>
<tr th:each="skill : ${currentSkills}"><td th:text="${skill.name}"/></tr>
</table>
</div>
<div th:if="${currentSkills == null}">
You don't have any skills
</div>

If currentSkills is a list, you can use the #lists utility like so (which is more correct than the above code since it also takes into account the possibility where the object is not null but is empty):

 <div  th:if="!${#lists.isEmpty(currentSkills)}">
<table>
<tr th:each="skill : ${currentSkills}"><td th:text="${skill.name}"/></tr>
</table>
</div>
<div th:if="${#lists.isEmpty(currentSkills)}">
You don't have any skills
</div>

You could do the same if currentSkills is an array by just replacing #lists with #arrays.

Note that in both cases isEmpty() returns true whether the object is null or has zero items.

How to do a th:if statement in thymeleaf?

*{host} is the same as ${server.host} (because server is your form's th:object). To compare it with something else you can use that same expression. Something like:

th:if="${server.host == test.host}"

EDIT:
does this do what you want?

<table>
<tr th:each="t: ${test}" th:if="${server.host == t.host}">
<td th:text="${t.Status}" />
<td th:text="${t.host}" />
<td th:text="${t.version}" />
</tr>
</table>

How to do a if else in thymeleaf inside a loop that populate a table

Just change your code to:

<tr th:each="nodeInfo : ${listOfData}">
<td th:if="${nodeInfo.name} == null">This is the value if the name is null</td>
<td th:if="${nodeInfo.name} != null">This is the value if the name is NOT null</td>
</tr>

Or even more consisely you could write:

<tr th:each="nodeInfo : ${listOfData}">
<td th:if="!${nodeInfo.name}">This is the value if the name is null</td>
<td th:if="${nodeInfo.name}">This is the value if the name is NOT null</td>
</tr>

which works because ${nodeInfo.name} is evaluated to true when name is not null

You could also explore the use of th:unless instead of using !=

Check out this part of the documentation for more details.

How can I use multiple conditions (and, or) in thymeleaf th:if

You can combine them

th:if="${lang == 'A' OR lang == 'B'}"


Related Topics



Leave a reply



Submit