Java Template EnginesIn the current
Infoweek.ch magazine I discuss Java-based template engines. One of the earliest tools in this category was
Webmacro. However, the problem at the time was, that Webmacro was published under Gnu Public License, which obviously makes it unusable for many (if not most) projects, even open source projects. So a team of developers started
Apache Velocity. Meanwhile also a new project
Freemarker offers interesting functionality in the open source Java domain.
Templates?Templating is an alternative approach for code generation opposed to methods like JSP or XSLT. Generally spoken, templates define the fixed structure of the (textual) output and the parts that should be replaced during runtime. To give an example:

This is a template written in Velocity Template Language (VTL): First, "static" information, i.e., text is sent to output without change. Variables can be declared using the
$variable syntax. These variables are exchanged at runtime with the desired values. The exchange of information between the Java class and the template engine happens using a context. In the Java class objects can be put into the context and in the template these objects can be accessed.
In the first simple examples:
$firstname, this object is a
String. The next example:
$person illustrates a more complex example:
Person is actually a Java class and in the template instance variables or methods can be accessed.
Finally simple logic can be performed, typically to iterate lists using the
#foreach commands or checking conditions using
#if.
The Java code to work with Velocity is fairly simple:

After initialisation, the Velocity context can be used to exchange data between application and template. Generally arbitrary objects can be put into the context. Calling the
mergeTemplate method eventually renders the result.
Advantages of Template Engines and Application ScenariosTemplate engines like Velocity can be used for various code-generation scenarios; typical examples are HTML, SQL and Java code generation. Thus Velocity is used in O/R mapping tools, UML tools for code generation and so on. A significant advantage of template-based HTML generation is, that cooperation of developer and designer is easier compared to XSLT-based approaches. However languages like XSLT are more powerful when the data and generated output is less structured.
Also the "learning curve" of tools like Velocity is very smooth, so results can be produced quickly. My suggestion is to get an overview on these technologies when a decision for code-generation tools is required in a project.
Webmacro, Velocity and Freemarker?All three projects have a significant user community. My impression is, that the community is most active in Velocity and Freemarker. These two projects have a similar, but in details different approach that is worth evaluating. E.g., Freemarker apparently has better support for rendering XML data using templates, Velocity seems to have a larger user-base on the other hand.
For more details check out the
infoweek.ch article (German).