Friday, September 9, 2011

FreeMarker vs Velocity ( FTL vs VM)


In Association with Amazon.in

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 as 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.


Template Engine: Template + Data-Model = Output

Open Source Template engines for the Java platform:

Name
Version
Purpose
URL
License
Velocity
1.7
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


What a Template Engine can do ?

Template Engines are most popularly used for creating HTML, in some variant or another, as the view in an MVC architecture. Creating a document always involves merging data and a template. The template is static, the data is dynamic. After processing the data and the template the template engine produces an output document. Based on the instructions in the template all or some of the data provided will be part of the output. 
 
Usually, you will provide the data to the template engine in the form of a collection or container, such as a HashMap. Most template engines provide special classes for this, commonly named context or model. FreeMarker provides a SimpleHash class (among others), Velocity offers a VelocityContext class for feeding data to the template engine.

The basic usage pattern of both is very simple:
  1. Instantiate an engine or template,
  2. Feed it the data, and
  3. Call a parse function to create the output. 
     
      Both template engines support the use of the Writer interface to write the output to, giving you a wide range of options to capture the output. 
 
FreeMarker also offers a conversion tool to convert Velocity templates to FreeMarker templates. This will save you a lot of time if you end up migrating from Velocity to FreeMarker. You can download this tool from http://freemarker.org/usCavalry.html

After download this use the following command for conversion:

java -jar cavalry.jar

If the
is not specified, it will output to stdout.

Basic Syntax Comparison:


FreeMarker
Velocity
Expression
Hello ${name}
Hello $name
If statement
<#if ${engineName} = ="FreeMarker" >
      Hello, ${engineName}
<#else>
       Hello, other world! 
#if ( $engineName = = "Velocity" )
      Hello, $engineName 
#else
      Hello, other world! 
#end
Loop
<#list sequence as item>
    ...
#foreach ( $item in $sequence )
    ...
#end
Include
<#include "/common/header.ftl">
#include ("/common/header.vm")

Now we will see some examples that uses FreeMarker and Velocity.

Example1: Web Page

Eample2: Email

Eample3: Process XML Data

Download the Samples from :



Where FreeMarker and Velocity are used ?

In Alfresco (DM), FreeMarker is extensively used for displaying Space/Folder, Content metadata with different templates, for sending Email notification, for displaying html content through WebScript.

In Alfresco Share which is built on Spring Surf Platform uses FreeMarker on a Presentation tier.

In Alfreso WCM, FreeMarker is extensively used for defining dynamic look and feel as well as layout of contents.

In Liferay, Both FreeMarker & Velocity are used for in Themes, Web Content templates and Sending Email.

FreeMarker and Velocity comparison:

Feature
FreeMarker
Velocity
Iteration
Yes
Yes
Conditional processing
Yes
Yes
Macros
Yes
Yes
Includes other templates
Yes
Yes
Parses included templates
Yes
Yes
Executes embedded Java code
No
Only if the objects are available in the context supplied to Velocity


In Association with Amazon.in

No comments:

Post a Comment