BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Thymeleaf 2.0: Performance, th:switch, Line Numbers, Template Fragments, New DOM API

Thymeleaf 2.0: Performance, th:switch, Line Numbers, Template Fragments, New DOM API

This item in japanese

Thymeleaf is a XML/HTML template engine for Java, whose main goal is to provide a well-formed way of creating templates. Thymeleaf 2.0 includes a lot of new features, including improvements for developers who create Thymeleaf extensions. Here's a quick run down of what's new.

Performance

The Thymeleaf internal architecture was been redesigned and the template execution engine was been completely rewritten. This means big performance improvements. Execution time from one benchmark now takes 1.26ms from the previous 4.19ms.

th:switch/th:case

The expression specified in th:switch is evaluated and compared with the result of expressions in the inner th:case attributes. The default option is specified as *. On previous versions, developers would have had to specify a th:if on each p element.

	<div th:switch="${user.role}">
		<p th:case="'admin'">Administrator</p>
		<p th:case="*">User</p>
	</div>

 

th:remove="all-but-first"

Creating a template with tables normally means you will have to remove the extra placeholder rows. The new th:remove="all-but-first" makes this easy to accomplish. Previously, developers would have to put th:remove="all" on all the table rows that needs removal.

	<table th:remove="all-but-first">
		<tr th:each="user : ${users}">
			<td th:text="${user.name}">John</td>
		</tr>
		<tr>
			<td>Joe</td>
		</tr>
		<tr>
			<td>Mike</td>
		</tr>
	</table>

 

Line Number Information

One of the most requested features is to output line number information when Thymeleaf encounters errors parsing templates. This is now available thanks to the new template engine.

DOM Selectors

Thymeleaf 1.1 allowed the inclusion of template fragments by specifying XPath expressions. Due to the template engine rewrite, Thymeleaf 2.0 has replaced this XPath support with what is called "DOM Selectors". A DOM Selector allows you to select a specific region of the DOM, and allows a subset of the XPath syntax.

  • /x means direct children of the current node with name x.
  • //x means children of the current node with name x, at any depth.
  • x[@z='v'] means elements with name x and an attribute called z with value "v".
  • x[@z1='v1' and @z2='v2'] means elements with name x and attributes z1 and z2 with values "v1" and "v2", respectively.
  • x[i] means element with name x positioned in number i among its siblings.
  • x[@z='v'][i] means elements with name x, attribute z with value "v" and positioned in number i among its siblings that also match this condition.

 

Template Fragments

The new template engine allows developers to process fragmentary templates instead of complete XML documents. For example, no DOCTYPE or document root is required, and the template could just be a <div> block.

Generalized Cache Infrastructure

Thymeleaf 2.0 has updated the cache infrastructure to give developers control over what caches are used and how they should work. The new ICacheManager interface defines an extension point that will allow users to specify their own implementation of ICache. The StandardCacheManager is included as the default implementation of ICacheManager.

New XHTML DTDs

The new th:switch and th:case attributes require a change in the Thymeleaf DTDs. Here are the new DTDs.

	<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-3.dtd">
	<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-transitional-thymeleaf-3.dtd">
	<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-frameset-thymeleaf-3.dtd">

	<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-3.dtd">
	<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-transitional-thymeleaf-spring3-3.dtd">
	<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-frameset-thymeleaf-spring3-3.dtd">

 

Replaced Java DOM API

Thymeleaf 2.0 has removed the Java DOM API from its architecture and replaced it with its own DOM representation. The new DOM is customized specifically for Thymeleaf and is simpler than the old standard. It also includes optimizations that allow Thymeleaf to process templates faster with fewer resources.

IProcessor

In Thymeleaf 1.1, processors are classified in two groups: attribute processors and tag processors. In Thymeleaf 2.0, the new DOM allows a generalization of this schema, so that attribute processors and tag/element processors are now children of the new general IProcessor interface. This means that processors can now be applied to any DOM node, like elements, text nodes, comments, etc.

Generalized Template Modes

 

Thymeleaf 2.0 has generalized the template modes, which allows Thymeleaf extension developers to create their own template modes beyond the six standard template modes (XML, VALIDXML, XHTML, VALIDXHTML, HTML5 and LEGACYHTML5). The template modes must now be specified at template resolver configuration as Strings instead of using the now-deprecated TemplateMode enum.

For more information, visit the Thymeleaf website and changelog.

Rate this Article

Adoption
Style

BT