DEV Community

dss99911
dss99911

Posted on • Originally published at dss99911.github.io

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">
Enter fullscreen mode Exit fullscreen mode

Variable Expression

Display Variable Value

Use th:text to display variable values:

<h4 th:text="${user.name}"></h4>
Enter fullscreen mode Exit fullscreen mode

Conditional Display

th:if Attribute

Display content conditionally:

<p th:if="${param.error}">
    Bad Credentials: ${param.error}
</p>
Enter fullscreen mode Exit fullscreen mode

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

Reference


Originally published at https://dss99911.github.io

Top comments (0)