HTML.Template.java

How to use HTML.Template

The <tmpl_var> tag

First, make a template - a normal HTML file with a few extra tags. The simplest is <tmpl_var>, so we'll start with that. We'll call the file test.tmpl.

<html>
<head><title>Test Template</title></head>
<body>
My Home Directory is <tmpl_var home>
<p>
My current working directory is set to <tmpl_var cwd>
</body>
</html>

Now create a small java program Test.java:

import HTML.Template;

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

			// Set some parameters
			t.setParam("home", System.getProperty("user.home"));
			t.setParam("cwd", System.getProperty("user.dir"));

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

If all goes well, you should get output that looks something like this:

My Home Directory is /home/some/directory
My current working directory is set to /some/directory

Escaping HTML metacharacters

If your data contains HTML metacharacters, there may be cases where displaying these characters as they are will cause problems with your output. eg:

<input type="text" name="name" value="<tmpl_var name>">

You can use the escape attribute of the <tmpl_var> tag to do this. eg:

<input type="text" name="name" value="<tmpl_var name escape>">

If name were equal to something like sam"my, then the first case would have bombed because of HTML's idea of a double quote. The second case would give you the expected result.

Setting default values

As of version 0.1.1, you can set default values for your template variables, which will be used if no value has been set through the setParam method.
<TMPL_VAR name="var_name" default="my value">
If var_name is set using setParam (or setParams), then it's value will be whatever it was set to, else it will be "my value" (without the quotes).

Preventing java/template variable name mismatches

If you set die_on_bad_params to true in your Template's constructor, then you will not be allowed to use setParam on variables that do not exist in your template. This value is false by default (for backwards compatibility), but may change to true in later versions for compatibility with HTML::Template