Search code examples
javaspringthymeleaf

If/else in thymeleaf


Is there a better way to do the following in thymeleaf?

<div th:if="${status} == 404">
    Special display for 404 page!
</div>

<div th:if="${status} != 404">
    Not a 404 page!
</div>

For example, in django it's as simple as:

{% if status == 404 %}
    Special display for 404 page!
{% else %}
    Not a 404 page!
{% endif %}

Solution

  • 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.