FreeMarker
 
 
 
More about FreeMarker
When you're writing servlets to generate dynamic web pages, how do you keep the HTML separate from your Java code, so that graphic designers can change the HTML without your having to change your classes?
FreeMarker solves this problem by encapsulating HTML in templates.
 

The source code for a template is an HTML document that contains instructions for including dynamically-generated data. These instructions are simple and unobtrusive (so that the graphic designers can still do their work), but powerful enough to let you use data structures of arbitrary complexity. It's easy to generate tables of data, and you can use "if" and "switch" statements to generate conditional HTML. Control structures (lists, if-else structures, and switch-case structures) can be nested as in any other programming language.

 
Sample code
Freemarker Syntax : The examples below shown below demonstrate how Freemarker has been embedded into HTML. Four tags have been used to show the same: Function, If Else, Switch case and assignment. 
 
If- Else:
An if-else structure can be represented as below:
<if condition>     Some HTML...
</if>

Or like this:
<if condition>
    Some HTML...
<else>

Some other HTML...
</if>
The condition  should be a Boolean expression. For example:
<if !(color == "blue" || color == "violet")>
    You have an unusual favorite color.
</if>
<if foo && (!(bar || baz) || elephant)>
    Tweedledee.
</if>
Since undefined, null, and empty values evaluate to false, it is convenient to use idioms like the following: <if error>
    <p>Your request cannot be processed because of the following error:<br>
   
<else>
    (display results...)
</if>
Since undefined, null, and empty values are considered equal, the following two statements are equivalent, whether foo is defined or not:
<if foo> ... </if>
 
<if foo != ""> ... </if>

Function:
You can define a function as shown below :
<function functionName(argName1, argName2...)>
    Some HTML...
</function> 
A function can have zero or more arguments. The function name and argument names must be legal identifiers. You can call a function like this:
<call functionName(arg1, arg2...)>
The arguments must be valid expressions.
For example:
<function showLink(url, image, alt)>
    <a href="">
    <if preferences.showImages>
        <img src="" border="0" alt="">
    <else>
       
    </if>
    </a>
</function>
 
...some HTML...
 
<call showLink(urls.home, images.home, "Home")>
 
...more HTML...
Functions are defined at compile time, so it's fine to call a function that's defined further down in the template. Make sure your function names don't conflict with your variable names; the template processor may get confused if they do.
Switch Case:
A switch-case structure has the same form as in Java: <switch variable>
    <case value1>
        HTML for case 1.
        <break>
    <case value2>
        HTML for case 2.
        <break>
    <default>
        HTML for the default case.
        <break>
</switch>
You can have as many cases as you like. The values value1, value2, etc., must be valid expressions. The <default> case is optional. The rules for what gets executed are the same as in Java; if you don't end a case with <break>, it will fall through to the next case.
For example:
<switch animal.name>
    <case "elephant">
    <case "rhinoceros">
        This is the HTML for large animals.
        <break>
    <case "squirrel">
    <case "gecko">
        This is the HTML for small animals.
       <break>
    <case favoriteAnimal.name>
        This is the HTML for the user's favorite animal.
        <break>
    <default>
        This is the HTML for other animals.
        <break>
</switch>
Assignment
Normally, a template's data will come from its data model. However, sometimes it's convenient to assign a value to a variable in a template. You can do so using this syntax:
<assign variable = expression>
Dots aren't allowed in the name of the variable you assign to. The expression can be any valid expression. The equals sign is optional syntactic sugar.
Assignment is mainly useful for making your templates more readable. For example:
<assign foo = "This is the constant value of foo">
<assign bar = some.long.variable.name>
The assignment is performed at run-time. In the second example, if some.long.variable.name is undefined, bar will be set to null.
Custom Functions
Following are certain custom functions for string manipulation in FreeMarker templates:
         String length determination:
<assign n = string_lib.length (s)> assigns to n the length of string s.          Substring extraction: <assign s = string_lib.substring (t, “2”,  “4”)> assigns to s the 2-character substring of t beginning at position 2. (The last parameter is the index of the position immediately after the end of the substring sought.)
·         String searching:
<assign index = string_lib.index (s, “zz”,"3")> assigns to index the (left end of the) leftmost occurrence, at or after index 3, of “zz” in S. Similarly, <assign index = string_lib.lastIndex (s, “zz”)> assigns to index the (left end of the) last occurrence of “zz” in s.
New Functions of run-time FreeMarker
The run-time FreeMarker template mechanism includes several new functions:
Regular expression matching, via string_lib.re_match. This function takes two arguments, a string and a regular expression against which to match it. The function returns a non-null scalar on success, and a null scalar on failure.
e.g. < if string_lib.re_match (date, "Sunday") >The date matches Sunday </if>
String replacement, via string_lib.replace. This function takes three parameters: the target string, the substring in the target string to be replaced, and the replacement string. It returns a string whose value is the target string with the leftmost occurrence replaced.
e.g.<assign newString = string_lib.replace (oldString, "hello", "howdy")>
replaces the first occurrence of hello in oldString with howdy and assigns the result to newString.
Integer comparison, via integer_lib.compare. This function takes three parameters: a string containing an integer (the first parameter for comparison), a string containing the comparison operator (any one of the six standard relational operators), and a string containing the second integer. The function returns a non-null scalar if the comparison succeeds, and a null scalar if not.
e.g.<if integer_lib.compare (i, "<", "30")> is less than 30.</if>
Integer arithmetic, via integer_lib.op. This is similar to the comparison function, except that it is used for arithmetic. Its second parameter can be any one of the five operators "+", "-", "*", "/" and "%". It returns a string containing the integer value of the result.
 
As an example, you can write the following FreeMarker code to get every third row in red:
 
<assign count = "0">
<table>
<list myList as element>
<tr>
   <td <if integer.compare (integer_lib.op (count, "%","3"), "==", "0")>
         style="background-color: red"
   </td>
</tr>
<assign count=integer_lib.op(count, "+", "1")>
</td>
</td>
 

Note: All arguments to FreeMarker methods must be strings, although the methods themselves can interpret the values as being of other types. That is why even the literal integers in the above example are in quotes.

Click here to know more about the various data items that you can use in FreeMarker programming, in meta-templates.
For More information on Freemarker visit the website  http://freemarker.sourceforge.net