Template

 

This library might be useful for Java servlet programmers. It is totally based on recursion. It supports if-then-else, for, iterator, variable, string constant. The limitation of this template library is that the maximum template file size is 32Kb. There is one way we can overcome this limitation. As ranab.tpl.Template class does not close the java.io.OutputStream we can load multiple template files sequentially.

Tag Syntax
Tag syntax for putting custom/variable Key value pair (properties/hash table) in the template files:

The customized templates should have key embedded in the form of ${mykey} anywhere within the file.

The API loadFile(OutputStream out, Map hash) will replace the ${mykey} with corresponding myvalue from the hashtable.
For example,  if myvalue is “Hello World”
 
<html>
<head><title></head></title>
<body>
<h1>${mykey}</h1>
<body>
</html>

 
<html>
<head><title></head></title>
<body>
<h1>Hello World</h1>
<body>
</html>

The actual replacement is done using toString() function. What that means is that, in case of any other object in myvalue, it gets the string representation.

For example,  if myvalue = new Integer(122);
 
<html>
<head><title></head></title>
<body>
<h1>You are visitor number ${mykey}</h1>
<body>
</html>

 
<html>
<head><title></head></title>
<body>
<h1>You are visitor number 122</h1>
<body>
</html>

If the hash table does not contain relevant mykey, the ${mykey} in the template will be ignored. We can also access null value using ${NULL}.

Example:
 
<html>
<head><title> </title></head>
<body>
  <h1>Welcome to ${comp_name}</h1>
</body>
</html>

If comp_name does not have a corresponding value in the hash table, the output would be
 
<html>
<head><title> </title></head>
<body>
  <h1>Welcome to </h1>
</body>
</html>

The Block Construct
In the following sections, we define Block as a chunk of HTML code/javascript/css along with any of the constructs defined in this doc.
 

The var Construct
Any object is defined to be a var.

.size
The suffix .size has a special meaning. It returns the size of the container.

.last
The last element of the vector. In case of any other object it returns the object itself.

[n]
One can get the value at a particular index, n using $(var[n]). If n is not an integer ${var[n]} = null.
In case of multi-dimensional array we have to use ${var[i,j,k]}.

If the object var-hash under consideration is not a vector, then the following rules:
  ${Var.size} = new Integer(1)
 ${Var[n]}   = var-hash if n == 0
             = null if n not equals 0

In case of vector var-hash,
 ${Var.size} = new Integer(var-templ.size())
 ${Var[n]}   = var-temp.elementAt(n) if 0 <= n < var-templ.size
             = null otherwise

In case the ${var} is not found in the hash table,
 ${Var.size} = new Integer(0)
 ${Var[n]}   = null
 

The if-then-else Construct

The template may contain if-then-else logic. The corresponding grammar is as follows:

 ${IF}[whitespace]*{condition}[whitespace]*{block}[{block}]

 whitespace – any combination of “ “, “\t”, “\r”, “\n”, “\f”
 condition  - var|String ==|!=|IN var|String
 var        - ${String}

The last optional section in the grammar represents the optional else part
Null value can be represented using ${NULL}.

Example:
 
<html>
<head><title></title></head>
<body>
${IF}{${user_name} == Administrator} {
 Customization Page}{
 Welcome ${user_name}
}
</body>
</html>

If user_name == Administrator
 
<html>
<head><title></title></head>
<body>
 Customization Page
</body>
</html>

If user_name == XYZ
 
<html>
<head><title></title></head>
<body>
 Welcome XYZ
</body>
</html>

For Construct

${FOR}[whitespace]*{initblock}[whitespace]*{block}

initblock  - String IN var

Suppose the HTML template writer needs to write a template which would be used by a servlet to display the number of distinct shirt sizes available and display the size names in the form of a list. The template would be of the following form:
 
 
<html>
<head><title></title></head>
<body>
<form method=POST action=/servlet/xyz>
Total number of sizes = ${fittings_size.size}

<select>
${FOR}{ABC IN ${fittings_size}}{
<option value=${ABC}>${ABC}</option>}
</select>

</form>
</body>
</html>

If the distinct shirt-sizes are “M”, “L”, “XL”, “XXL” the servlet writer would write code similar to the following form:

 Hashtable hash = new Hashtable();
 Vector vect = new Vector();
 vect.add("M");
 vect.add("L");
 vect.add("XL");
 vect.add("XXL");
 hash.put("fittings_size", vect);

and the html output would be as follows:
 
<html>
<head><title></title></head>
<body>
<form method=POST action=/servlet/xyz>
Total number of sizes = 4

<select>

<option value=M>M</option>
<option value=L>L</option>
<option value=XL}>XL</option>
<option value=XXL>XXL</option>
</select>

</form>
</body>
</html>

Iterator Construct

Very often there is a requirement to show up indexed value from multiple vectors in a tabular representation.
For example, consider the following :

Item_list = { "shirt", "flowers", "cups", "pencil"}
Quantity = { 1, 2, 12, 3}
Units = {Number, Bouquet, Dozen, Box}

And the table should be something like
 
shirt 1 Number
flowers 2 Bouquet
cups 12 Dozen
pencil 3 Box

In order to achieve this, we would require an iterator type construct, which allows us to iterate items from a vector.

The grammar is as follows:

 ${ITR}[whitespace]*{initblock}[whitespace]*{block}

 initblock  - String[whitespace]+ var|string TO var|string
 

In the initblock, the first var|string is the start index and the second var|string is the iteration count.

Example 1:

The template for the example detailed above would be as follows:
 
<html>
<table>
${ITR}{INDEX 0 TO ${Item_list.size}}{
<tr>
<td>${Item_list[${INDEX}]}</td>
<td>${Quantity[${INDEX}]}</td>
<td>${Unit[${INDEX}]}</td>
</tr>}
</table>
</html>

The resultant output is:
 
<html>
<table>
<tr>
<td>shirts</td>
<td>1</td>
<td>Number</td>
</tr>
<tr>
<td>flowers</td>
<td>2</td>
<td>Bouquet</td>
</tr>
<tr>
<td>cups</td>
<td>12</td>
<td>Dozen</td>
</tr>
<tr>
<td>pencils</td>
<td>3</td>
<td>Box</td>
</tr>
</table>
</html>

Example 2:

Let us consider an even more complicated scenario:

Suppose we are required to display the attribute and corresponding list of values of an item.
So the variable issues in this example are:

The corresponding template in that case would be as follows:
 
<form>
${ITR}{IND1 0 TO ${prop_list.size}}{
<b>${prop_list[${IND1}]}</b>
<select>
${FOR}{IND2 IN ${value[${IND2}] }}{
<option value=${IND2}>${IND2}</option>
</select>
<br>}
}
</form>

Suppose the servlet retrieves the following for a specific item :

 prop_list={"Size", "Color"}
 value={{"XL", "L", "M"},
        {"Red", "Blue", "Green"}}

Then the generated html code would be :
 
<form>
<b>Size</b>
<select>
<option value=XL>XL</option>
<option value=L>L</option>
<option value=M>M</option>
</select>
<br>

<b>Color</b>
<select>
<option value=Red>Red</option>
<option value=Blue>Blue</option>
<option value=Green>Green</option>
</select>
<br>
</form>


 

Dynamic Variable Generation

Variables can be formed dynamically.

For example
 var1 = var2
 var2 = "Hello World"

So in template ${${var1}} will be evaluated as Hello World.


Please send comments to rana_b@yahoo.com