Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly ignore/hide elements with Thymeleaf templates

Tags:

thymeleaf

I end up with a lot of code like:

<section layout:fragment="content" th:remove="tag">
    <p>content section</p>
</section>

and

<section th:switch="${session['SOME_KEY'" th:remove="tag">
    <section th:case="${'BadCredentialsException'}" th:remove="tag">
        Invalid username/password
    </section>
    <section th:case="${'UsernameNotFoundException'}" th:remove="tag">
        Invalid username/password
    </section>
    <section th:case="${'DisabledException'}" th:remove="tag">
        Account is blocked, contact your admin
    </section>
    <section th:case="*" th:remove="tag">
        Something went wrong
    </section>
</section>

Note the th:remove="tag" to not display the in this case section tags as I don't neem them.

Is this the proper way to do this?

like image 548
Marcel Overdijk Avatar asked Apr 09 '14 05:04

Marcel Overdijk


1 Answers

Since Thymeleaf 2.1 there's a sythetic th:block tag available. It's container attribute that is removed after execution (see documentation).

So you end up with code like this :

<th:block layout:fragment="content">
    <p>content section</p>
</th:block>
like image 121
michal.kreuzman Avatar answered Oct 29 '22 15:10

michal.kreuzman