How to use HTML.Template

The <tmpl_loop> tag

The <tmpl_loop> tag is a bit more complicated than the other tags. It allows you to create a section of text that will be displayed repeatedly for every item in the loop control variable. Inside the <tmpl_loop>, you place <tmpl_var>s.

In the java, you use a Vector that contains a list of Hashtables, each representing one record/group of <tmpl_var>s to be displayed in the loop.

The loop iterates over the vector, replacing variables in the text block with their parameter values from each Hashtable.


Create a template called test-loop.tmpl:

<tmpl_loop employee_info>
	Name: <tmpl_var name><br>
	Job: <tmpl_var job><br>

And the corresponding java code

import HTML.Template;
import java.util.*;

public class Test3 {
	public static void main(String [] args) {
		try {
			// Create the Template object
			Template t = new Template("test-loop.tmpl");

			// Set some parameters
			Vector v = new Vector();
			Hashtable h = new Hashtable();
			h.put("name", "Philip");
			h.put("job", "programmer");
			h = new Hashtable();
			h.put("name", "Bob");
			h.put("job", "Web designer");
			h = new Hashtable();
			h.put("name", "John");
			h.put("job", "Manager");

			t.setParam("employee_info", v);

		} catch(Exception e) {

Compile and run the code. If all goes well, you should get output like this:

	Name: Philip<br>
	Job: programmer<br>

	Name: Bob<br>
	Job: Web designer<br>

	Name: John<br>
	Job: Manager<br>

You can also put conditions inside your loops:

<tmpl_loop employee_info>
	Name: <tmpl_if name><tmpl_var name><tmpl_else>(none)</tmpl_if><br>
	Job: <tmpl_if job><tmpl_var job><tmpl_else>(none)</tmpl_if><br>

In this case, if any of the records have an empty name or job, the output will be (none) for those cases.

Loop context variables

If you set loop_context_vars to true in your Template's constructor, then you will have access to five special variables in all your loops. These are:

True for the first run of the loop, false otherwise
True for the last run of the loop, false otherwise
True for every other iteration of the loop - a loop starts at 1
True if both __FIRST__ and __LAST__ are false
Which iteration is currently on. Starts at 1.(new in 0.1.1)

You may use these like any other <TMPL_VAR> inside your loop.

Accessing variables from outside your loops

By default, loops only give you access to variables defined within the loop, however, if you set global_vars to true in your Template's constructor, your loops will have access to all variables declared above your loop. ie, you can access variables defined in the top level of your template, or any loops that contain the current loop, but not in loops that are unattached to the current loop.

A lower level variable will override a higher level one. The following should explain this whole concept:

datastructure in java:


<tmpl_var Var1>, <tmpl_var Var2>, <tmpl_var Var3>
<tmpl_loop Loop1>
  <tmpl_var Var1>, <tmpl_var Var2>, <tmpl_var Var3>
<tmpl_loop Loop2>
  <tmpl_var Var1>, <tmpl_var Var2>, <tmpl_var Var3>

With global_vars off:

Hello, World, 
  , How are you, Cool
  , , Great

With global_vars on:

Hello, World
  Hello, How are you, Cool
  Hello, , Great