perldoc > HTML::Template

๐Ÿ“„ NAME

HTML::Template โ€” Perl module to use HTML-like templating language

๐Ÿš€ Quick Reference

Use CaseCommandDescription
๐Ÿ†• Create a template objectmy $t = HTML::Template->new(filename => 'file.tmpl')Load a template from a file
๐Ÿ“ Set a variable$t->param(NAME => 'value')Assign a value to a TMPL_VAR
๐Ÿ”„ Set a loop data$t->param(LOOP => [{...}, {...}])Pass an array of hashrefs for a TMPL_LOOP
๐Ÿ–จ๏ธ Output final HTMLprint $t->output()Generate the template output
๐Ÿ” Check if a parameter exists$t->query(name => 'VAR')Returns type (VAR, LOOP, etc.) or undef
๐Ÿงน Clear all parameters$t->clear_params()Reset all param values to undef
โšก Lazy variable (code ref)$t->param(VAR => sub { expensive() })Only evaluated when used in template

๐Ÿ“‹ 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></head>
<body>
My Home Directory is <TMPL_VAR NAME=HOME>
<p>
My Path is set to <TMPL_VAR NAME=PATH>
</body>
</html>

Now you can use it in a small CGI program:

#!/usr/bin/perl -w
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 and print the template output
print "Content-Type: text/html\n\n", $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 to 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>, <TMPL_ELSE> and <TMPL_UNLESS>. 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 separate design โ€” the HTML โ€” from the data, which you generate in the Perl script.

This module is licensed under the same terms as Perl. See the LICENSE section below for more details.

๐Ÿ“˜ Tutorial

If you're new to HTML::Template, I suggest you start with the introductory article available on Perl Monks:

http://www.perlmonks.org/?node_id=65642

โ“ FAQ

Please see HTML::Template::FAQ

๐Ÿ’ก 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 programs 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

๐Ÿ”ค TMPL_VAR

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

You can also specify the value of the parameter as a code reference in order to have "lazy" variables. These sub routines will only be referenced if the variables are used. See LAZY VALUES for more information.

Attributes

The following attributes can also be specified in template var tags:

๐Ÿ”„ TMPL_LOOP

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

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

Example template:

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

Perl code:

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

Output:

Name: Sam
Job: programmer

Name: Steve
Job: soda jerk

Nested loops work as expected. Each loop gets an array reference of hash references. Inside a loop, only the loop's variables are visible unless global_vars is used.

๐Ÿ“‚ TMPL_INCLUDE

<TMPL_INCLUDE NAME="filename.tmpl">

Includes a template directly at the point of the tag. The file can be absolute or relative. Search order: path of enclosing file, HTML_TEMPLATE_ROOT environment variable, path option, then direct open(). Maximum include depth is 10 (configurable via max_includes).

๐Ÿค” TMPL_IF

<TMPL_IF NAME="PARAMETER_NAME"> ... </TMPL_IF>

Conditionally includes a block if the parameter is true (in Perl sense). If the parameter is a loop name, the block is output if the loop has at least one row.

๐Ÿ”€ TMPL_ELSE

<TMPL_IF NAME="PARAMETER_NAME"> ... <TMPL_ELSE> ... </TMPL_IF>

Alternate block for <TMPL_IF>. End with </TMPL_IF>.

๐Ÿšซ TMPL_UNLESS

<TMPL_UNLESS NAME="PARAMETER_NAME"> ... </TMPL_UNLESS>

Opposite of <TMPL_IF> โ€” block is output if parameter is false or undefined. Works with <TMPL_ELSE>.

๐Ÿ“ Notes

HTML::Template's tags are meant to mimic normal HTML tags, but they can "break the rules" (e.g., <img src="<TMPL_VAR IMAGE_SRC>">). The NAME= is optional. Tags can also be written as HTML comments: <!-- TMPL_VAR NAME=PARAM1 -->.

๐Ÿ”ง Methods

๐Ÿ†• new

Create a new Template object:

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

Alternate source types: scalarref, arrayref, filehandle. Convenience methods: new_file, new_scalar_ref, new_array_ref, new_filehandle. Also new(type => 'filename', source => 'file.tmpl').

The environment variable HTML_TEMPLATE_ROOT affects relative paths.

Options

๐Ÿ” Error Detection Options
๐Ÿ’พ Caching Options
๐Ÿ“ Filesystem Options
HTML::Template
๐Ÿ“„ NAME ๐Ÿš€ Quick Reference ๐Ÿ“‹ Synopsis ๐Ÿ“– Description ๐Ÿ“˜ Tutorial โ“ FAQ ๐Ÿ’ก Motivation ๐Ÿท๏ธ The Tags ๐Ÿ”ง Methods

Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-27 17:42 @216.73.216.199
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^