The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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>, <TMPL_LOOP>, <TMPL_INCLUDE>, <TMPL_IF> and <TMPL_ELSE>. 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, loops and branches 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 LICENSE 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.

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

<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.

<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!

Inside a <TMPL_LOOP>, the only variables that are usable are the ones from the <TMPL_LOOP>. The variables in the outer blocks are not visible within a template loop. For the computer-science geeks among you, a <TMPL_LOOP> introduces a new scope much like a subroutine call.

<TMPL_INCLUDE NAME="filename.tmpl">

This tag includes a template directly into the current template at the point where the tag is found. The included template contents exactly as if its contents were physically included in the master template.

NOTE: Currently, each <TMPL_INCLUDE> must be on a seperate line by itself.

<TMPL_IF NAME="CONTROL_PARAMETER_NAME"> </TMPL_IF>

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 for Perl - like '1' - then the block is included in the output. If it is not defined, or given a false value - like '0' - then it is skipped. The parameters are specified the same way as with TMPL_VAR.

Example Template:

<TMPL_IF NAME="BOOL">
  Some text that only gets displayed if BOOL is true!
</TMPL_IF>

Now if you call $template->param(BOOL => 1) then the above block will be included by output.

<TMPL_IF> </TMPL_IF> blocks can include any valid HTML::Template construct - VARs and LOOPs and other IF/ELSE blocks. Note, however, that intersecting a <TMPL_IF> and a <TMPL_LOOP> is invalid.

Example !!!THIS EXAMPLE WILL NOT WORK!!! :

<TMPL_IF BOOL>
   <TMPL_LOOP SOME_LOOP>
</TMPL_IF>
   </TMPL_LOOP>

WARNING: Much of the benefit of HTML::Template is in decoupling your Perl and HTML. If you introduce numerous cases where you have TMPL_IFs and matching Perl if()s, you will create a maintainance problem in keeping the two synchronized. I suggest you adopt the practice of only using TMPL_IF if you can do so without requiring a matching if() in your Perl code.

<TMPL_ELSE>

You can include an alternate block in your TMPL_IF block by using TMPL_ELSE. NOTE: You still end the block with </TMPL_IF>, not </TMPL_ELSE>!

Example:

<TMPL_IF BOOL>
  Some text that is included only if BOOL is true
<TMPL_ELSE>
  Some text that is included only if BOOL is false
</TMPL_IF>

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 mod_perl and 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.

If the environment variable HTML_TEMPLATE_ROOT is set and your filename doesn't begin with /, then the path will be relative to the value of $HTML_TEMPLATE_ROOT. Example - if the environment variable HTML_TEMPLATE_ROOT is set to "/home/sam" and I call HTML::Template->new() with filename set to "sam.tmpl", the HTML::Template will try to open "/home/sam/sam.tmpl" to access the template file.

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 STDERR. 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. Defaults to 1.

  • cache - if set to 1 the module will cache in memory the parsed templates based on the filename parameter and modification date of the file. This only applies to templates opened with the filename parameter specified, not scalarref or arrayref templates. Cacheing also looks at the modification times of any files included using <TMPL_INCLUDE> tags, but again, only if the template is opened with filename parameter. 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 using cache yields a 50% performance increase under mod_perl, more if you use large <TMPL_LOOP>s. Cache defaults to 0.

  • blind_cache - if set to 1 the module behaves exactly as with normal cacheing but does not check to see if the file has changed on each request. This option should be used with caution, but could be of use on high-load servers. My tests show blind_cache performing only 1 to 2 percent faster than cache.

  • associate - this option allows you to inherit the parameter values from other objects. The only rwquirement for the other object is that it have a param() method that works like HTML::Template's param(). A good candidate would be a CGI.pm query object. Example:

    my $query = new CGI;
    my $template = HTML::Template->new(filename => 'template.tmpl',
                                       associate => $query);

    Now, $template->output() will act as though

    $template->param('FormField', $cgi->param('FormField'));

    had been specified for each key/value pair that would be provided by the $cgi->param() method. Parameters you set directly take precedence over associated parameters.

    You can specify multiple objects to associate by passing an anonymous array to the associate option. They are searched for parameters in the order they appear:

    my $template = HTML::Template->new(filename => 'template.tmpl',
                                       associate => [$query, $other_obj]);

    The old associateCGI() call is still supported, but should be considered obsolete.

  • no_includes - if you know that your template does not have TMPL_INCLUDE tags, then you can set no_includes to 1. This will give a small performance gain, since the prepass for include tags can be skipped. 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 guaranteed 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.

FREQUENTLY ASKED QUESTIONS

In the interest of greater understanding I've started a FAQ section of the perldocs. Please look in here before you send me email.

I want support for <TMPL_XXX>! How about it?

Maybe. I definitely encourage people to send me their ideas for HTML::Template, but you should realize that I have a strong desire to keep HTML::Template simple. Please be ready to explain to me how the new tag fits in with HTML::Template's mission to provide a fast, lightweight system for using HTML templates.

NOTE: Offering to program said addition and provide it in the form of a patch to the most recent version of HTML::Template will definitely have a softening effect of my resolve!

I found a bug, can you fix it?

That depends. Did you send me the VERSION of HTML::Template, a test script and a test template? If so, then almost certainly.

<TMPL_VAR>s from the main template aren't working inside a <TMPL_LOOP>! Why?

This is the intended behavior. <TMPL_LOOP> introduces a separate scope for <TMPL_VAR>s much like a subroutine call in Perl introduces a separate scope for "my" variables. If you need to have a variable from the main template work inside a loop you'll need to manually provide the value for each iteration of the loop.

Why do you use /[Tt]/ instead of /t/i? It's so ugly!

Simple - the case-insensitive match switch is very inefficient. According to _Mastering_Regular_Expressions_ from O'Reilly Press, /[Tt]/ is faster and more space efficient than /t/i - by as much as double against long strings. //i essentially does a lc() on the string and keeps a temporary copy in memory.

When this changes, and I'm told it will, I will gladly use //i. Believe me, I realize [Tt] is hideously ugly.

BUGS

I am aware of no bugs - if you find one, email me (sam@tregar.com) with full details, including the VERSION of the module, a test script and a test template demonstrating the problem. Please see the FREQUENTLY ASKED QUESTIONS above before you mail me!

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, Bug Reports, Optimizations and Ideas have been generously provided by:

Richard Chen
Mike Blazer
Adriano Nagelschmidt Rodrigues
Andrej Mikus
Ilya Obshadko
Kevin Puetz
Steve Reppucci
Richard Dice
Tom Hukins
Eric Zylberstejn
David Glasser

Thanks!

AUTHOR

Sam Tregar, sam@tregar.com

LICENSE

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 388:

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