Search code examples
htmlspring-bootthymeleaf

Thymeleaf: Delete the div that contains iterating attribute (th:each) after looping through or conditional attribute (th:if) after evaluating


In Thymeleaf, is it possible to delete the wrapper div which contains the conditional loop? Suppose the following code in a SpringBoot application, I intend to erase the first and the second divs after evaluating the condition and looping through the errors list.

<div th:if="${errors != null}">
    <div th:each="error : ${errors}">
        <div th:utext="${error}"></div>
    </div>
</div>

I fancy achieving this without using JavaScript.


Solution

  • You could also use a <th:block /> (which isn't rendered). Also, no need to separate the iteration and text. You should be able to generate the same code like this:

    <th:block th:if="${errors != null}">
        <div th:each="error : ${errors}" th:utext="${error}" />
    </th:block>
    

    (I prefer <th:block /> to th:remove="tag".)