th:each is a Thymeleaf namespace attribute that repeats an HTML element for every item in a collection. Each iteration binds the current item to a template variable. section 2.1.3
A typical form uses th:each to build repeated controls from model data:
- Place th:each on the element to repeat, such as <div th:each="ingredient : ${wrap}">. section 2.1.3
- Read the current item through the bound variable, such as ingredient.id and ingredient.name. section 2.1.3
- Combine th:each with th:value, th:field, and th:text to render functional, state-preserving checkbox controls. section 2.1.3
<h3>Designate your wrap:</h3>
<div th:each="ingredient : ${wrap}">
<input th:field="*{ingredients}" type="checkbox"
th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
Shows a complete th:each pattern for rendering wrap ingredients as checkbox inputs and labels. section 2.1.3
