Template Engines for Java WebApps (Velocity Vs FreeMarker Vs Thymeleaf Vs Rythm)

In many projects, developed using Java technologies Java Server Pages (JSPs) was the standard for the presentation layer for a long time. Among many other things JSPs has the features of a template processor. The biggest shortcoming of this technology is the possibility to insert business logic in the presentation code. Thus, we can insert into the JSP document scriptlets or even Java code blocks. Although this can help us in certain situations, the code becomes rapidly complex and hard to maintain. Because of shortcomings such as this one, JSP lost many followers in favor of other template engines, which are used in more and more projects, increasing the developers" productivity and the quality of the products.

In this article I will try to analyse four popular template engines available freely for commercial use. The four template engines are Apache Velocity, FreeMarker, Thymeleaf and Rythm.

Introduction

Velocity is a project distributed under the Apache Software License, which benefits from great popularity among the Java applications developers. Like other template engines, Velocity was designed to enable the webmasters and the Java developers to work in parallel.

FreeMarker is a product that has reached maturity, distributed under a BSD license. Similar to the Apache Velocity project, FreeMarker offers complex functionalities designed for assisting web developers. It was designed for efficiently generating HTML pages, and not only that. Furthermore as the creators of the project state, this is a generic software product that can be used for text generation, ranging from HTML to source code.

Thymeleaf is a Java library distributed under the ٢nd version of Apache License with the main goal to create templates in an elegant way. The most suitable use case for Thymeleaf is generation of XHTML / HTML٥ documents within a web context. However this tool can be used in offline environments as well, being able to process XML documents.

Rythm is a template engine for Java applications distributed under the Apache License version 2.0 and described by its author as being a general purpose product, easy to be used and super-fast. Similarly to other template processors, we can process with it HTML documents, XML documents, SQL scripts, source code, emails and any other kind of formatted text. Rythm was inspired by the .Net Razor project, due to its simple and elegant syntax.

Template Language

Apache Velocity enjoys its own scripting language called Velocity Template Language (VTL) which is powerful and flexible. The authors of Apache Velocity proudly advertise that their product"s flexibility is limited only by the user"s creativity. VTL was created to offer the simplest and cleanest way for inserting dynamic content into a web page.

FreeMarker provides us with a strong templating language, called FreeMarker Template Language. With FTL we can define expressions, functions and macros within the templates that we write. Furthermore, we also have the possibility to use a rich library with predefined directives that give us the possibility to iterate data collections, include other templates, and much more.

Thymeleaf relies on a set of features called a dialect. The standard distribution comes with the Standard dialect and the SpringStandard dialect which allow us to create so-called natural templates. These can be correctly displayed by the browser even when we access them as static files. Consequently these documents can be viewed as prototypes.

Rythm was inspired by the .Net Razor project, due to its simple and elegant syntax. Rythm uses the special character @ to introduce all syntax elements.

Performances

Using Apache HTTP server benchmarking tool on machine with following specs:

Mac OS X Version 10.11.1
2,3 GHz Intel Core i7 Quad core
ava(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)
Apache Tomcat 7.0.53 with 512M RAM

Total time taken for processing 25.000 requests with a concurrency level of 25. (lower is better)
We optain following results:

Thymeleaf               4.147 seconds
Freemarker              2.637 seconds
Velocity                2.491 seconds
Rythm                   1.991 seconds

Conclusions

Both Velocity and FreeMarker are established products, which proved their worth in many successful projects offering decent performance. On the other hand Thymeleaf and Rythm are young projects that come with a new philosophy adapted to the current trends in web development. For instance, Thymeleaf excels at natural templating, while Rythm offers a clean syntax, easy to understand both for programmers and webmasters. We can conclude that choosing a template engine depends first of all on the project we need it for.