How to use HTML.Template

The <tmpl_if> tag

The <tmpl_if> tag allows you to include or not include a block of the template based on the value of a given parameter name. If the parameter is given a value that is true, then the block is included in the output. If the value is false, then it is skipped.

Parameter values for conditions are Strings in java, and not boolean values, hence, what evaluates to true and false needs to be specifically defined.

As of now, the following values evaluate to false:
"" - the empty string
"0" - the string containing only the number zero
null - the null value
An empty Vector - as of version 0.0.3, an empty vector also evaluates to false

All other String and Vector values evaluate to true.


Create a template with the following:

<tmpl_if name>
	Your name is <tmpl_var name>

And the corresponding java program:

import HTML.Template;

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

			// Set a parameter
			t.setParam("name", "Philip");

		} catch(Exception e) {

When you run the code, you should get the output:

	Your name is Philip

Now, comment out the line in the java source that says t.setParam("name", "Philip");, and run the code again.

This time you should get a blank output.


While the if block allows us to display some output if a condition is true, we may also need to display content if the condition is false. This can be done by using the optional else branch of the if block.

Now, change the above template to this:

<tmpl_if name>
	Your name is <tmpl_var name>
	You did not give a name

Again run the code with and without the setParam statement.