March 9, 2021
Hot Topics:

Add Velocity to Your Development

  • By Josh Lucas
  • Send Email »
  • More Articles »

What a Template Engine Should Be

In it's simplest form, a template engine takes data, processes it and then displays it. For example, the data found in a company's employee list can be shown in a simple HTML page on the company's intranet or it can be simplied and seen on a salespersons' cellphone while he is on the road. The use of templates is what enables programmers and designers to interact and utilize the data in the most appropriate way. A very good example of what a template engine should be is Velocity.

Velocity is an open-source Java-based template engine. It is part of the Jakarta project and boasts an active community of developers and users. It was initially created as a re-thinking of the WebMacro servlet framework but it has grown to have a variety of non-servlet uses.

Keep it Simple

The beauty of Velocity lies in its simple template syntax and the separation of content and data. It allows programmers to worry just about the code and designers to create the necessary look-and-feel without having to include all sorts of code inside the templates. This is a marked contrast from the traditional JavaServerPages (JSP) and PHP ways of creating pages. In those pages, programming logic is included in the pages and can cause confusion and difficulty in understanding and maintaining the pages. Velocity does this by forcing the use of at least two files, the template and a Java class. A quick example will help illustrate this distinction of data and display.

Putting Velocity to Use

First, we need to download and install Velocity if it isn't already on your computer. 1.3.1 has just been released and the examples in this article will be using that particular version. The released files can be found on Jakarta's server. Once you've downloaded and unpacked either the tarball or zip file, put the velocity-dep-1.3.1.jar in your CLASSPATH. This will allow the examples to be compiled and ran without difficulty.

The usual suffix for a Velocity template is .vm so let's create a file named hellovelocity.vm. This will be a very simple template which, can show if everything is installed correctly, and how Velocity forces the separation of content and presentation. Put the following in the file:

Hello $name, this is Velocity working!
Once you have saved the template, it will be time to open HelloVelocity.java. It's best to put this file in the same directory as the template. Here's the code that shows the Velocity magic:

import java.io.StringWriter;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

public class HelloVelocity
    public static void main(String[] args) throws Exception
        /*  first, get and initialize an engine  */
        VelocityEngine engine = new VelocityEngine();

        /*  next, get the Template  */
        Template template = engine.getTemplate( "hellovelocity.vm" );

        /*  create a context and add data */
        VelocityContext context = new VelocityContext();
        context.put("name", "World");

        /* now render the template into a StringWriter */
        StringWriter writer = new StringWriter();
        template.merge( context, writer );

        /* display the results */
        System.out.println( writer.toString() );    

Once both files are saved and ready, compile the class and run. If all goes according to plan, you should see:

Hello World, this is Velocity working!

Page 1 of 2

This article was originally published on April 30, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date