Velocity or FreeMarker?

Two open source Java-based template engines compared


Template engines can be applied to a wide variety of development scenarios where you want to generate text based on specific processing rules, and are often used as the view component in MVC applications. In this article, Jeroen van Bergen explains where template engines fit into your application architecture and shows you some of the operations common to all template engines. Finally, he compares the two leading Java template engines, Velocity and FreeMarker, in terms of power, ease of use, and performance.

Template engines are used in Java development scenarios where it is necessary to automatically generate text and format it according to specific processing rules. The two most popular open source Java template engines currently are Velocity and FreeMarker. While Velocity has been the leading template engine for a long time, FreeMarker has begun to overtake it, offering a speed and sophistication you won't always find in Velocity.

In this article I briefly introduce you to template engines, explaining where they fit into your Java application architecture and showing you some basic operations common to them all. I then compare Velocity and FreeMarker based on power, ease of use, and performance.

This discussion is primarily intended for readers new to template engines who want to learn what they're used for and get some help choosing between the two leading open source options.

What is a template engine?

A template engine is a component that takes fixed text and data as input, integrates these following certain processing rules, and outputs a text document containing the data. Template engines are very useful for tasks such as creating dynamic Web pages, documents, and e-mails, and can also be used to generate source code.

Template engines are used mostly as the view component in an MVC architecture. The template engine encourages good separation between the actual view logic and the rendered view, which is considered a Good Thing. Using a template engine involves some overhead in building the application, but most developers see a quick return on investment when the application has to be changed. The separation of view from the logic makes it easy to update the look and feel of the application by adjusting the templates, rather than having to re-write the source code. The relationship is similar to that of CSS and HTML, where a change in style requires a small edit to the CSS file, rather than many revisions to the HTML.

Overview of open source Java template engines

As is often the case when developing on the Java platform, you have many Java-based template engines to choose from. I have tried to compile a comprehensive list of all Java template engines that are currently available. Some of these are real template engines in the sense that they have not been developed with a certain kind of application in mind. Some are specialized engines that aim to do a specific kind of document creation, such as decorating Web pages. If a template engine is not meant for general-purpose use you'll see an explicit description of the type of application it's intended for.

Table 1. Template engines for the Java platform

Velocity 1.5 General-purpose template engine Apache Software License
FreeMarker 2.3.10 General-purpose template engine BSD License
SiteMesh 2.3 Web page layout and decoration OpenSymphony License
TeaServlet 2.3 Web pages Tea License
Jamon 2.3.0 General-purpose template engine Mozilla Public License
WebMacro   General-purpose template engine GNU General Public License
1 2 3 Page 1