NAME

HTML::Template - Perl module to use HTML Templates from CGI scripts

SYNOPSIS

First you make a template - this is just a normal HTML file with a few extra tags, the simplest being <TMPL_VAR>

For example, test.tmpl:

<HTML>
<HEAD><TITLE>Test Template</TITLE>
<BODY>
My Home Directory is <TMPL_VAR NAME=HOME>
<P>
My Path is set to <TMPL_VAR NAME=PATH>
</BODY>
</HTML>

Now create a small CGI program:

use HTML::Template;

# open the html template
my $template = HTML::Template->new(filename => 'test.tmpl');

# fill in some parameters
$template->param('HOME', $ENV{HOME});
$template->param('PATH', $ENV{PATH});

# send the obligatory Content-Type
print "Content-Type: text/html\n\n";

# print the template
print $template->output;

If all is well in the universe this should show something like this in your browser when visiting the CGI:

My Home Directory is /home/some/directory My Path is set to /bin;/usr/bin

DESCRIPTION

This module attempts make using HTML templates simple and natural. It extends standard HTML with a few new HTML-esque tags - <TMPL_VAR> and <TMPL_LOOP>. The file written with HTML and these new tags is called a template. It is usually saved separate from your script - possibly even created by someone else! Using this module you fill in the values for the variables and loops declared in the template. This allows you to seperate design - the HTML - from the data, which you generate in the Perl script.

This module is licenced under the GPL. See the LICENCE section below for more details.

MOTIVATION

It is true that there are a number of packages out there to do HTML templates. On the one hand you have things like HTML::Embperl which allows you freely mix Perl with HTML. On the other hand lie home-grown variable substitution solutions. Hopefully the module can find a place between the two.

One advantage of this module over a full HTML::Embperl-esque solution is that it enforces an important divide - design and programming. By limiting the programmer to just using simple variables and loops in the HTML, the template remains accessible to designers and other non-perl people. The use of HTML-esque syntax goes further to make the format understandable to others. In the future this similarity could be used to extend existing HTML editors/analyzers to support HTML::Template.

An advantage of this module over home-grown tag-replacement schemes is the support for loops. In my work I am often called on to produce tables of data in html. Producing them using simplistic HTML templates results in CGIs containing lots of HTML since the HTML itself cannot represent loops. The introduction of loop statements in the HTML simplifies this situation considerably. The designer can layout a single row and the programmer can fill it in as many times as necessary - all they must agree on is the parameter names.

For all that, I think the best thing about this module is that it does just one thing and it does it quickly and carefully. It doesn't try to replace Perl and HTML, it just augments them to interact a little better. And it's pretty fast.

The Tags

Note: even though these tags look like HTML they are a little different in a couple of ways. First, they must appear entirely on one line. Second, they're allowed to "break the rules". Something like:

<IMG SRC="<TMPL_VAR NAME=IMAGE_SRC>">

is not really valid HTML, but it is a perfectly valid use and will work as planned.

<TMPL_VAR NAME="PARAMETER_NAME">

The <TMPL_VAR> tag is very simple. For each <TMPL_VAR> tag in the template you call $template->param("PARAMETER_NAME", "VALUE"). When the template is output the <TMPL_VAR> is replaced with the VALUE text you specified. If you don't set a parameter it just gets skipped in the output.

The "NAME=" in the tag is now optional, although for exensibility's sake I recommend using it. Example - "<TMPL_VAR PARAMETER_NAME>" is now acceptable.

<TMPL_LOOP NAME="LOOP_NAME"> </TMPL_LOOP>

The <TMPL_LOOP> tag is a bit more complicated. The <TMPL_LOOP> tag allows you to delimit a section of text and give it a name. Inside the <TMPL_LOOP> you place <TMPL_VAR>s. Now you pass to param() a list (an array ref) of parameter assignments (hash refs) - the loop iterates over this list and produces output from the text block for each pass. Unset parameters are skipped. Here's an example:

 In the template:

 <TMPL_LOOP NAME=EMPLOYEE_INFO>
       Name: <TMPL_VAR NAME=NAME> <P>
       Job: <TMPL_VAR NAME=JOB> <P>
      <P>
 </TMPL_LOOP>


 In the script:

 $template->param('EMPLOYEE_INFO', 
                  [ 
                   { name => 'Sam', job => 'programmer' },
                   { name => 'Steve', job => 'soda jerk' },
                  ],
                 );
 print $template->output();


 The output:

 Name: Sam <P>
 Job: programmer <P>
 <P>
 Name: Steve <P>
 Job: soda jerk <P>
 <P>

As you can see above the <TMPL_LOOP> takes a list of variable assignments and then iterates over the loop body producing output.

<TMPL_LOOP>s within <TMPL_LOOP>s are fine and work as you would expect. If the syntax for the param() call has you stumped, here's an example of a param call with one nested loop:

$template->param('ROW',[
                        { name => 'Bobby',
                          nicknames => [
                                        { name => 'the big bad wolf' }, 
                                        { name => 'He-Man' },
                                       ],
                        },
                       ],
                );

Basically, each <TMPL_LOOP> gets an array reference. Inside the array are any number of hash references. These hashes contain the name=>value pairs for a single pass over the loop template. It is probably in your best interest to build these up programatically, but that is up to you!

The "NAME=" in the tag is now optional, although for exensibility's sake I recommend using it. Example - "<TMPL_LOOP LOOP_NAME>" is now acceptable.

Methods

new()

Call new() to create a new Template object:

my $template = HTML::Template->new( filename => 'file.tmpl', 
                                    option => 'value' 
                                  );

You must call new() with at least one name => value pair specifing how to access the template text. You can use "filename => 'file.tmpl'" to specify a filename to be opened as the template. Alternately you can use:

my $t = HTML::Template->new( scalarref => $ref_to_template_text, 
                             option => 'value' 
                           );

and

my $t = HTML::Template->new( arrayref => $ref_to_array_of_lines , 
                             option => 'value' 
                           );

These initialize the template from in-memory resources. These are mostly of use internally for the module - in almost every case you'll want to use the filename parameter. If you're worried about all the disk access from a template file just use the cache option detailed below.

The three new() calling methods can also be accessed as below, if you prefer.

my $t = HTML::Template->new_file('file.tmpl', option => 'value');

my $t = HTML::Template->new_scalar_ref($ref_to_template_text, 
                                      option => 'value');

my $t = HTML::Template->new_array_ref($ref_to_array_of_lines, 
                                     option => 'value');

And as a final option, for those that might prefer it, you can call new as:

my $t = HTML::Template->new_file(type => 'filename', 
                                 source => 'file.tmpl');

Which works for all three of the source types.

You can modify the Template object's behavior with new. These options are available:

  • debug - if set to 1 the module will write debugging information to STDOUT. Defaults to 0.

  • die_on_bad_params - if set to 0 the module will let you call $template->param('param_name', 'value') even if 'param_name' doesn't exist in the template body. Be careful with this one - I can't think of any situations where this shouldn't be an error. Defaults to 1.

  • cache - if set to 1 the module will cache in memory the parsing of templates based on the filename parameter and modification date of the file. This only applies to templates opened with the filename parameter specified, not the scalarref or arrayref templates. Note that different new() parameter settings do not cause a cache refresh, only a change in the modification time of the template will trigger a cache refresh. For most usages this is fine. My simplistic testing shows that setting cache to 1 yields a 50% performance increase, more if you use large <TMPL_LOOP>s. Cache defaults to 0.

  • vanguard_compatibility_mode - if set to 1 the module will expect to see <TMPL_VAR>s that look like %NAME% instead of the standard syntax. If you're not at Vanguard Media trying to use an old format template don't worry about this one. Defaults to 0.

param

param() can be called in three ways

1) To return a list of parameters in the template :

my @parameter_names = $self->param();

2) To return the value set to a param :

my $value = $self->param('PARAM');

3) To set the value of a parameter :

# For simple TMPL_VARs:
$self->param('PARAM', 'value');

# And TMPL_LOOPs:
$self->param('LOOP_PARAM', 
             [ 
              { PARAM => VALUE_FOR_FIRST_PASS, ... }, 
              { PARAM => VALUE_FOR_SECOND_PASS, ... } 
              ...
             ]
            );

clear_params()

Sets all the parameters to undef. Useful internally, if nowhere else!

output()

output() returns the final result of the template. In most situations you'll want to print this, like:

print $template->output();

When output is called each occurance of <TMPL_VAR NAME=name> is replaced with the value assigned to "name" via param(). If a named parameter is unset it is simply replaced with ''. <TMPL_LOOPS> are evaluated once per parameter set, accumlating output on each pass.

Calling output() is garaunteed not to change the state of the Template object, in case you were wondering. This property is mostly important for the internal implementation of loops.

BUGS

The <TMPL_LOOP> </TMPL_LOOP> matching code was difficult to write and debug. I think you can assume from that that its not perfect. If you have trouble with the module detecting the closing </TMPL_LOOP>, move it to its own line. If it's still crapping out, try moving the <TMPL_LOOP NAME="name"> to its own line as well.

Other than that I am aware of no bugs - if you find one, email me (sam@tregar.com) with full details, including the VERSION of the module and a test script / test template demonstrating the problem.

CREDITS

This module was the brain child of my boss, Jesse Erlbaum (jesse@vm.com) here at Vanguard Media. The most original idea in this module - the <TMPL_LOOP> - was entirely his.

Fixes and Bug Reports have been generously provided by:

Richard Chen

Thanks!

AUTHOR

Sam Tregar, sam@tregar.com

LICENCE

HTML::Template : A module for using HTML Templates with Perl

Copyright (C) 1999 Sam Tregar (sam@tregar.com)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

1 POD Error

The following errors were encountered while parsing the POD:

Around line 274:

=back doesn't take any parameters, but you said =back 4