Injecting a set of files in a Thymeleaf template while keeping the amount of tags at a minimum I use a Thymeleaf template to inject a set of files (which contain precalculated HTML) into one big HTML. The files with their metadata are stored in a map called files, the files content is stored in another map called content. Both maps share the same key which is basically an id of the files.

The relevant section of the template has these lines:

&lt;div th:each="f : ${files}" th:id="${f.key}"&gt;
    &lt;h1 th:text="${f.value.pageTitle}"&gt;&lt;/h1&gt;
        [(${content[ f.key ]})]
&lt;/div&gt;


The result after this template was processed is:

&lt;div id="1"&gt;
    &lt;h1&gt;Title of document with ID 1&lt;/h1&gt;
    Content of file with ID 1
&lt;/div&gt;
&lt;div id="2"&gt;
    &lt;h1&gt;Title of document with ID 2&lt;/h1&gt;
    Content of file with ID 2
&lt;/div&gt;


This works fine but in order to place the th:each I had to introduce a superfluous div. Can I get rid of the div in the template and have a result like this:

&lt;h1&gt;Title of document with ID 1&lt;/h1&gt;
Content of file with ID 1
&lt;h1&gt;Title of document with ID 2&lt;/h1&gt;
Content of file with ID 2
Thymeleaf has a mechanism for removing template fragments. You can use the th:remove="tag" attribute to remove the containing tag but not its children:

&lt;div th:each="f : ${files}" th:remove="tag"&gt;
    &lt;h1 th:text="${f.value.pageTitle}"&gt;&lt;/h1&gt;
    [(${content[ f.key ]})]
&lt;/div&gt;


The resulting output will not contain the div but the h1 element and the text node.