Thymeleaf Attributes
Explore how to use essential Thymeleaf attributes such as th:text for dynamic text, th:id for element IDs, and th:if/th:unless for conditional rendering. Understand iteration with th:each and the concept of attribute precedence. Discover preprocessing expressions to integrate dynamic content based on locale or variables, enhancing your Spring Boot web templates.
The Thymeleaf expressions we just saw can be used inside Thymeleaf attributes like the th:text attribute. This lesson will go over the most important attributes.
Element text content
The th:text attribute will place the result of the expression inside the tag it is declared on.
For example, assuming the username variable contains “Jane”:
<div th:text="${username}">Bob</div>
will render as:
<div>Jane</div>
Element id attribute
Similarly, the th:id attribute will add an id attribute.
<div th:id="|container-${userId}|"></div>
will render (userId will be 1 here) as:
<div id="container-1"></div>
Conditional inclusion
We can choose to render a tag subject to some condition using the th:if attribute. For example:
<div th:if="${user.followerCount > ...