NAME
HTML::SimpleTemplate - Quick and simple template library for Perl
SYNOPSIS
use
HTML::SimpleTemplate;
# A template object referencing a particular directory
my
$adminpages
=new HTML::SimpleTemplate(
"/templates/admin"
);
# A template object referencing files in this directory
my
$localpages
=new HTML::SimpleTemplate;
# Display this template file, passing no parameters
$localpages
->Display(
"welcome.tpl"
);
# Display this template file, passing the %account hash
$localpages
->Display(
"account.tpl"
,\
%account
);
# Display this template file, with a-la-carte parameter
$localpages
->Display(
"order.tpl"
,{
product
=>
"Perl mug"
});
DESCRIPTION
DISPLAY METHOD
Display(
$file
,[\
%variables
]);
The Display method parses the specified file according to the rules specified below. It can optionally be passed a second parameter, a reference to a hash, containing the variables used in the template.
When the SimpleTemplate object is created a template path can optionally be specified. If this is absent filenames specified to Display are relative to the current directory.
TEMPLATE SYNTAX
Hello
$name
!
?(
$name
eq
"George"
)
?
$rude
..you old fool!
?end
It's too late at night to think of witty examples!
?end
A template is a text file which may contain certain variable references and special commands. Variables are referenced by prefixing their name with the dollar symbol, as in Perl itself. Variable names must be alphanumeric, and must be followed by a non-alphanumeric character (including spaces and newlines) so the module knows where the name ends.
There area very small number of special commands, which all involve either a ? or an ! in the first column of the line.
CONDITIONALS
?
$SomeVariable
The variable evaluated as true
?
else
No it didn't
?end
?(
$Any
=~/perl evaluation that doesn't contain brackets/)
It was
?
else
It wasn't (
else
is optional by the way!)
?end
You may nest conditionals to your hearts content, but the layout makes it confusing if you have more than two or three. Anyway that kind of complex logic belongs in Perl, not in HTML.
INCLUDES
!header.tpl
!common/header.tpl
!errors/
$ErrorType
.tpl
When you include another template all of the variables in your current template remain available.
CALLBACKS
&InsertContent
(
$fish
)
Yes, you can call Perl inside your template. You can, of course, then call templates from inside that Perl. Very powerful, potentially very confusing...
COMMENTS
Saving the best for last, you can now put sensible comments in your HTML - and the page viewer doesn't get to see it!
# This section for stupid people only
?NameError
It appears you have failed to enter your name.
# You freakin' numbskull!
?end
NOTES
Clearly you cannot embed conditionals, callbacks etc in the middle of a line. You also can't have any plain content following a command. However, for HTML, which this is primarily designed for, that just isn't an issue.
By the way, if you're creating an HTML page, it works quite nicely to put that Content-type: text/html in the template if it makes you happy having it there.
AUTHOR
A Crawford, acrawford@ieee.org
SEE ALSO
HTML::Template