These examples illustrate some of the typical uses of Template Toolkit directives.
# include another template, passing "local" variables [% INCLUDE header title = 'Hello World' %] # process "config" file to set some variables [% PROCESS my_vardefs %] # iterate through 'userlist' list of hashrefs [% FOREACH user = userlist %] [% user.name %] [% user.email %] [% END %] # shorthand for above [% INCLUDE userinfo.html FOREACH user = userlist %] [% INCLUDE "$user.id/info.html" FOREACH user = userlist %] # conditional block [% IF graphics %] [% INCLUDE gratuitous_logo %] [% END %] # define a template block [% BLOCK table_row %] <td>[% name %]</td> <td>[% email %]</td> [% END %] # INCLUDE the defined block [% INCLUDE table_row name='me' email='me@here.org' %] [% INCLUDE table_row name='you' email='you@there.org' %] [% INCLUDE table_row FOREACH userlist %] # example of the 'format' plugin... [% USE bold = format('<b>%s</b>') %] [% bold('This is bold') %] # ...and any other plugins... [% USE myplugin %] [% myplugin.does_this %] [% myplugin.does_that(foo, bar, baz) %] # html filter 'escapes' characters '<', '>' and '&' [% FILTER html %] The value for Xyzzyx is < 100 ... [% END %] [% "$user.name showed that x < y && z > 0" FILTER html %] # define a block to be processed when 'myerror' is thrown [% CATCH myerror %] <b>A strange and mystical error has occurred.</b> <ul> [% e.type %]: [% e.info %] </ul> [% # report error to STDERR/logfile/etc., cleanup and stop %] [% ERROR "myerror occurred: $e.info" %] [% INCLUDE footer %] [% STOP %] [% END %] # throw a 'myerror' exception [% THROW myerror 'The warp core has been breached' %]
To install Template, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Template
CPAN shell
perl -MCPAN -e shell install Template
For more information on module installation, please visit the detailed CPAN module installation guide.