This page will try to cover some of the real basics of using the Perl Template Toolkit package (TT for short). I intend to discuss some basic syntax, give examples of some of the more commonly used commands, and a few tips. This is not intended to be a tutorial or reference for the Perl Template Toolkit. For more information, look at the following references:
perldoc Template::Manual::Intro
Basically, TT processes a file by reading lines in and spitting it back out verbatim, until it encounters a start tag. The start tag tells it to start looking for and interpretting TT commands, until a close tag occurs and it goes back to echoing the input verbatim, until another start tag is encountered, and so on. (Actually, it is possible to run TT in such a way that some processing is done even outside the start and close tags, but in general that is not done in Physics Dept.)
The basic start tag is [%
, but you may also encounter
[%#
, [%+
, and
[%-
. The basic closing tag is
%]
, with variants +%]
and
-%]
.
The [%
start tag starts a comment block.
Everything from the start tag to the next closing tag (any variant) is
considered a comment, and is ignored by TT and is not echoed to the output.
The '+' and '-' and vanilla variants are all pretty much the same, with the
only difference being how whitespace before or after the tag is treated.
The '+' variants retain whitespace before the opening tag or after the closing
tag, the '-' variants omit that same whitespace. The default vanilla version
([%
or %]
), can behave either way depending
on how the module is called; in Physics code they generally behave like the
'+' variants. For most purposes, the vanilla variants are fine. Also note
that you can mix and match variants on the starting and closing tags, e.g.
[%+ INCLUDE test.tt -%]
When inside the TT blocks, a # symbol means that everything after it, until the end of the line, is a comment. Note that [%# is different; when there is no space between the start tag and the # symbol, the entire template language block is treated as a comment.
Commands in the TT block are terminated by semi-colon, and can span
multiple lines. The last command before a closing tag does not require
the final semi-colon (although can have one); it is mandatory for all other
commands (including in a few places you might be inclined to leave it out, e.g.
after the ELSE
command). In general, extra whitespace is not
a problem.
Standard TT commands are generally all capital letters, although you will often see macros, variables, object methods on these variables, etc. which do not follow that format. TT is case sensitive.
You can use either single or double quotes to quote literals, and string literals must be quoted.
Some macros take arguments; these can be either positional and/or named depending on the macro, or both. A positional argument looks like a standard C language function call, e.g. something like
[% myfunct(1,2,"abc") %]
1
, 2
, and abc
are all positional
args. Which variables they are assigned to depends on the way the macro
was defined.
Named arguments look like
[% myfunct(name="abc",version=1,copy=2) %]
name
,
version
, and copy
variables is explicit. It is
also often possible to mix notations in a single call, e.g.
[% myfunct(1,name="abc",copy=2); %]
When processing a block (via INCLUDE
or PROCESS
)
variables from the calling block can generally be accessed. You can also
do a temporary variable assignment (sort of an argument list) via name like
[% INCLUDE myblock version=1, name="abc"; %]
.
One of the simplest and commonest TT commands is GET
, which
just evaluates the argument to the command and outputs it. Indeed, it is
so common, that the GET
keyword can be omitted, so basically
if there is just an expression on the line, e.g. a string literal or a
variable, it is evaluated and printed.
TT also has a variable assignment mechanism, although is actually probably
a bit more like an output redirection. Any command can be preceded by a
variable name and an equal sign, and the output of the command will be
suppressed and assigned to that variable instead (This is actually the
SET
command, but the command word is optional.) When combined
with the GET
command (especially when both keywords are omitted),
you get something that looks like a typical C assignment statement. E.g.
[% x = y + 1; %]
Note that in addition to variables defined the variable assignment mechanism above, the perl script invoking the template can (and often will) provide variables, usually perl objects. Data members and methods can be accessed via the dot notation, e.g.
[% x.result = obj.fancyMethod(5,4,7); %]
[% x=[1,2,3]; y=2; z=x.$y; a=x.1;
s1={}; s1.blue="0000ff"; s2=s1.blue; b="blue"; s3=s2.$b; %]
GET expression
: The expression is evaluated and
printed. The GET
keyword is usually omitted. Example
[% "The answer is "; 3+4; GET "\n"; %]
SET var = COMMAND
:
The command is evaluated and the output is assigned to the named variable.
COMMAND
can be any TT command.
Note: This does not work well with postfix conditional, due to
order of operations, as you will generally interpret something like
[% x=1 IF y==2; %]
1 IF y==2
, which returns 1 or nil depending on y. So basically,
avoid the postfix conditional clauses.
DEFAULT var = expression
If the named variable is not defined or not true (in the Perlish sense), the
expression is evaluated and assigned to the variable. Multiple variables and
expressions can be given in same command, separated by whitespace.
INSERT file
The file or block is inserted at the
current position. No template processing of file is done.
INCLUDE file
The file or block is inserted at the
current position. The file is run through the template processor, and has
access to all current variables (plus any temporary variable assignments
given on command line). However, the variables are provided as local copies,
so changes in the included block do not propagate back up. In particular,
any macros defined in the block do not affect the calling block.
PROCESS file
Basically the same as
INCLUDE
, but the variables are not localized first, so changes
to variables made in the included block propagate back out to the calling block.
In particular, if the included block contains macro definitions, these are
now defined for the calling block.
BLOCK
allows one to create named or anonymous blocks in the
same file. These can be included or processed, or can use with set for here
document type of effect, e.g.
[% tmp = BLOCK %]
This is some long text
More text
And more
[% END %]
END
command.
IF
, ELSIF
, ELSE
, END
allow for conditional processing. There must be an END
command
for each IF
command. Conditional operators are
The following is a short list of hints and tricks for working with TT that I have found useful:
[% GET x+y/z IF z > 0; %]
[% obj.set_result( a + 3 * b ); %]
[% tmp= a + 3 * b; obj.set_result(tmp); %]