Thymeleaf Template Engine Guide
Introduction
Thymeleaf is a modern server-side Java template engine for web and standalone environments. It’s commonly used with Spring Framework.
Setup
Add the Thymeleaf namespace to your HTML:
<html xmlns:th="http://thymeleaf.org">
Variable Expression
Display Variable Value
Use th:text to display variable values:
<h4 th:text="${user.name}"></h4>
Conditional Display
th:if Attribute
Display content conditionally:
<p th:if="${param.error}">
Bad Credentials: ${param.error}
</p>
Common Thymeleaf Attributes
| Attribute | Description |
|---|---|
th:text |
Replace text content |
th:if |
Conditional rendering |
th:unless |
Negative conditional rendering |
th:each |
Loop iteration |
th:href |
Dynamic link |
th:src |
Dynamic source |
th:value |
Form field value |
th:object |
Form backing object |
th:field |
Form field binding |
Comments