Introducing Thymeleaf | Thymeleaf Template | Thymeleaf Template Engine

In this tutorial, we'll discuss:
  • What is Thymeleaf?
  • What kind of templates can Thymeleaf process?
  • What is a Thymeleaf template?
  • Thymeleaf Engine
  • Where's a Thymeleaf template processed?
Learn Thymeleaf at https://www.javaguides.net/p/thymeleaf-tutorial.html

What is Thymeleaf?

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS, and even plain text.
The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates.
It's commonly used to generate HTML views for web apps.
Thymeleaf template engine is commonly used with the Spring MVC framework to develop web applications.
Learn Spring boot at https://www.javaguides.net/p/spring-boot-tutorial.html
Learn Spring MVC at https://www.javaguides.net/p/spring-mvc-tutorial.html 

What kind of templates can Thymeleaf process?

Out-of-the-box, Thymeleaf allows you to process six kinds of templates, each of which is called a Template Mode:
  1. HTML
  2. XML
  3. TEXT
  4. JAVASCRIPT
  5. CSS
  6. RAW
There are two markup template modes (HTML and XML), three textual template modes (TEXT, JAVASCRIPT and CSS) and a no-op template mode (RAW).

Let's briefly look into these 6 template modes:
  • The HTML template mode will allow any kind of HTML input, including HTML5, HTML 4 and XHTML.
  • The XML template mode will allow XML input.
  • The TEXT template mode will allow the use of a special syntax for templates of a non-markup nature. Examples of such templates might be text emails or templated documentation.
  • The JAVASCRIPT template mode will allow the processing of JavaScript files in a Thymeleaf application.
  • The CSS template mode will allow the processing of CSS files involved in a Thymeleaf application.
  • The RAW template mode will simply not process templates at all. It is meant to be used for inserting untouched resources (files, URL responses, etc.) into the templates being processed.

What is a Thymeleaf template?

  • Well, Thymeleaf template can be an HTML page with some Thymeleaf expressions.
  • It can also include dynamic content from Thymeleaf expressions.
For example, here is the HTML page with Thymeleaf attributes and expressions:
We use Thymeleaf expressions to access Java variables, objects, and Spring beans.

Thymeleaf Engine

Thymeleaf Engine will parse Thymeleaf Template. It uses Java model data to replace the positions marked on the Thymeleaf Template to create a new text in the HTML page.

The below screenshot shows how the Thymeleaf Engine process the Themeleaf template:

Where's a Thymeleaf template processed?

  • Well, in web applications, the Thymeleaf is processed on the server.
  • Results are included in the HTML and returned to the browser.

In Spring MVC web applications, the Spring MVC controller returns views (Thymeleaf template) as the result and that will be again rendered on the browser.

The below screenshot shows a typical request flow in Spring MVC application and the Spring MVC controller returns views (Thymeleaf template) as the result and that will be again rendered on the browser:

Related Thymeleaf Tutorials and Examples

  1. Introducing Thymeleaf | Thymeleaf Template | Thymeleaf Template Engine
  2. Thymeleaf Example with Spring Boot
  3. How to Add CSS and JS to Thymeleaf
  4. Add Bootstrap CSS to Thymeleaf
  5. How to handle null values in Thymeleaf?
  6. How to Loop a List by Index in Thymeleaf
  7. Thymeleaf Array Example - Array Index, Array Iteration
  8. Thymeleaf Enums Example
  9. Thymeleaf If Else Condition Example
  10. Thymeleaf Switch Case Example


Comments