BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Thymeleaf: XML/HTML Template Engine for Java

Thymeleaf: XML/HTML Template Engine for Java

This item in japanese

Bookmarks

The first stable version of Thymeleaf, version 1.0.0, has been released on July 17, 2011. Thymeleaf is an XML/XHTML/HTML5 template engine that works for web and non-web applications. It's an open source Java library distributed under Apache License 2.0 and created by Daniel Fernández, author of the Jasypt Java encryption library. Thymeleaf is a replacement for JSP and other template engines like Velocity and FreeMarker. It comes in two versions, the Standard dialect and the SpringStandard (Spring MVC 3) dialect, and can be extended through the creation of custom dialects.

The primary goal of Thymeleaf is to provide a well-formed way of creating templates that can be correctly displayed by browsers, and therefore also work as static prototypes. It allows you to create fully validating XML and HTML templates. Instead of writing logic or code, developers add tag attributes to the templates. The tag attributes then execute predefined logic on the DOM (Document Object Model). Thymeleaf is also very extensible. It allows you to define your own sets of template attributes, giving you the ability to evaluate custom expressions and apply custom logic. This basically means Thymeleaf can also act as a template engine framework.

So how does Thymeleaf compare with popular template engines like Velocity and FreeMarker? Thymeleaf is different as it uses natural templating. This means Thymeleaf's template syntax does not break the document's structure, and the template remains a valid XML document. Templates can also serve as working prototypes, and Thymeleaf will take care of replacing static values at run-time. Velocity and FreeMarker work as sequential text processors, and this allows them to process any text based template. Thymeleaf on the other hand is an XML processor using DOM. This limitation of working only on XML-based formats allows Thymeleaf to take advantage of features specific to XML and web environments. Note that since Thymeleaf uses an XML DOM parser, it is not advisable to use for processing large XML files.

Here's an example of printing a message in Velocity, FreeMarker and Thymeleaf.

Velocity: <p>$message</p>

FreeMarker: <p>${message}</p>

Thymeleaf: <p th:text="${message}">Hello World!</p>

Notice that Thymeleaf works off the paragraph tag and supports hard-coded text that gets replaced at runtime.

Thymeleaf attributes can be in the form of expressions, of which there are four types: variable expressions, selection expressions, text externalization expressions, and URL expressions. Variable expressions are Object-Graph Navigation Language (OGNL) expressions, or Spring EL if using SpringStandard, executed on the context map. Selection expressions are like variable expressions, except they will be executed on the parent object. Text externalization expressions allow us to retrieve content from external sources like .properties files. URL expressions add context and session information to URLs. Read "Getting started with the Standard dialects" for examples of Thymeleaf expressions.

You can get started by downloading Thymeleaf from SourceForge and reading the Using Thymeleaf Tutorial. If you use Maven, use org.thymeleaf for groupId and thymeleaf for the artifactId. Minimum dependencies are Java SE 5, ognl 3.0, javassist 3.14.0-GA, and slf4j 1.6.1. Visit the official Thymeleaf documentation and forum for more information.

Rate this Article

Adoption
Style

BT