HTML.Template.java

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.

Example

Create a template called test-loop.tmpl:

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

And the corresponding java code Test3.java:

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");
			v.addElement(h);
			h = new Hashtable();
			h.put("name", "Bob");
			h.put("job", "Web designer");
			v.addElement(h);
			h = new Hashtable();
			h.put("name", "John");
			h.put("job", "Manager");
			v.addElement(h);

			t.setParam("employee_info", v);

			System.out.print(t.output());
		} catch(Exception e) {
		}
	}
}

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


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

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

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

You can also put conditions inside your loops:

<tmpl_loop employee_info>
	<p>
	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>
	</p>
</tmpl_loop>

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:

__FIRST__
True for the first run of the loop, false otherwise
__LAST__
True for the last run of the loop, false otherwise
__ODD__
True for every other iteration of the loop - a loop starts at 1
__INNER__
True if both __FIRST__ and __LAST__ are false
__COUNTER__
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:

Template:

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

With global_vars off:

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

With global_vars on:

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