Cover | Table of Contents | Colophon
[%
... %] tags indicate variable
values. Everything else is fixed text that passes through the
processor untouched.People of [% planet %], your attention please. This is [% captain %] of the Galactic Hyperspace Planning Council. As you will no doubt be aware, the plans for development of the outlying regions of the Galaxy require the building of a hyperspatial express route through your star system, and regrettably your planet is one of those scheduled for destruction. The process will take slightly less than [% time %]. Thank you.
[%
... %] tags indicate variable
values. Everything else is fixed text that passes through the
processor untouched.People of [% planet %], your attention please. This is [% captain %] of the Galactic Hyperspace Planning Council. As you will no doubt be aware, the plans for development of the outlying regions of the Galaxy require the building of a hyperspatial express route through your star system, and regrettably your planet is one of those scheduled for destruction. The process will take slightly less than [% time %]. Thank you.
--define options to provide values
for variables. If the preceding template is stored in the
destruction.tt file in the current directory,
the following command processes it:$ tpage --define planet=Earth \ > --define captain="Prostetnic Vogon Jeltz" \ > --define time="two of your earth minutes" \ > destruction.tt
People of Earth, your attention please. This is Prostetnic Vogon Jeltz of the Galactic Hyperspace Planning Council. As you will no doubt be aware, the plans for development of the outlying regions of the Galaxy require the building of a hyperspatial express route through your star system, and regrettably your planet is one of those scheduled for destruction. The process will take slightly less than two of your earth minutes. Thank you.
{ at the beginning and
} at the end. If you want a variable interpolated,
you write it the way you would in Perl. If you need to make a loop,
you can use any of the Perl loop constructions. All the Perl built-in
functions are available.http://www.plover.com/~mjd/perl/Template/ or
from CPAN (http://search.cpan.org/dist/Text-Template/).http://search.cpan.org/dist/HTML-Template/).http://search.cpan.org/dist/Template-Toolkit/
(which is where most people download it).http://www.template-toolkit.org, this site
offers the latest stable version, as well as a number of other
goodies such as native packages of the Template Toolkit for Debian
GNU/Linux, Mac OS X (for installation using Fink), and Microsoft
Windows (for installation using ActiveState's Perl
Package Manager).$ perl Makefile.PL $ make $ make test $ make install
$ perldoc Template # should always work $ man Template # does not work everywhere
# TT2
Alias /tt2/images/ /usr/local/tt2/images/
Alias /tt2/docs/ /usr/local/tt2/docs/html/
Alias /tt2/examples/ /usr/local/tt2/examples/html/
<Directory /usr/local/tt2/>
Options Indexes
AllowOverride None
Order allow,deny
Allow from all
</Directory>http://www.template-toolkit.org/docs.html.Template module, the main
module for using the Template Toolkit from Perl.Template module; and
in a mod_perl-enabled Apache web server using the
Apache::Template module.$ tpage infile
> file redirect operator (if
your operating system supports it, or something similar) to save the
output
into another file:$ tpage infile > outfile
$ tpage nosuchfile file error - nosuchfile: not found at /usr/bin/tpage line 60.
--define,
which allows you to provide values for template variables embedded in
the document. We saw this earlier in Example 1-1
where it processed the Vogon form letter:$ tpage --define planet=Earth \ > --define captain="Prostetnic Vogon Jeltz" \ > --define time="two of your earth minutes" \ > destruction.tt
[% and
%], but the TAGS directive can
be used to change them if you don't like these. The
TAGS directive takes either one or two arguments.
The single-argument version expects the name of a predefined tag set.
For example, the star set replaces the tag
delimiters with [* and *]:[% TAGS star %] People of [* planet *], your attention please.
TAGS two arguments, they define the
start and end tag markers that you want to use. For example, if
you're processing plain text, you might find
something like this more lightweight and easier to type:[% TAGS { } %]
People of {planet}, your attention please.[% TAGS <tt: > %] <p>People of <tt:planet>, your attention please.
--start_tag, --end_tag, and
--tag_style options. From a Perl script, the
corresponding configuration options for the
Template module are START_TAG,
END_TAG, and TAG_STYLE. For
Apache::Template, the TT2Tags
option can be used with one or two arguments, as per the
TAGS directive.[% planet %]
--define option of the
tpage command, but it is also possible to set a
variable's value inside a template:[% planet = 'Magrethea' %] People of [% planet % ], your attention please.
process( ) method.friends and a hash of terms as
template variables.use Template;
my $tt = Template->new( );
my $input = 'friends.tt';
my $vars = {
friends => [ 'Ford Prefect', 'Slartibartfast' ],
terms => {
sass => 'know, be aware of, meet, have sex with',
hoopy => 'really together guy',
frood => 'really, amazingly together guy',
},
];
$tt->process($input, $vars)
|| die $tt->error( );Your friends are: [% FOREACH friend IN friends -%] * [% friend %] [% END -%] You know the following terms: [% FOREACH term IN terms.keys.sort -%] [% term %]: [% terms.$term %] [% END -%]
PROCESS,
IF, or FOREACH and tell the
template processing engine to do something.[% name = 'Arthur Dent' %]
[% planet = { name = 'Earth' } %]
Welcome [% name %] of [% planet.name %].SET and
GET keywords like so:[% SET name = 'Arthur Dent' %]
[% SET planet = { name = 'Earth' } %]
Welcome [% GET name %] of [% GET planet.name %].PROCESS directive is one of the simplest. It loads
another template file, processes the contents, and inserts the
generated output in the calling template:[% PROCESS header %]
INCLUDE_PATH
option, which allows you to specify one or more directories where
your template files can be found. This allows you to specify your
templates with simple names such header, rather
than full file paths such as
/home/dent/templates/lib/header, for example.INCLUDE_PATH and not
PROCESS_PATH becomes obvious when we mention that
there is also an INCLUDE directive. The
INCLUDE directive and related
INCLUDE_PATH option have been part of the Template
Toolkit, and the Text::Metatext module that
preceded it, from the very beginning. The PROCESS
directive, on the other hand, was added at a later date, and was able
to reuse the INCLUDE_PATH option for the same
purposes.PROCESS and
INCLUDE is revealed in Chapter 2. For now it suffices to know that
INCLUDE is most often used when you want to pass
variable values that should remain local to that one template:USE directive, eliminating the need to
write a Perl wrapper program.http://www.cygwin.com<html>
<head>
<title>Arthur Dent: Greet the Planet</title>
</head>
<body bgcolor="#FF6600">
<h1>Greet the Planet</h1>
<p>
Hello World!
</p>
<hr />
<div align="middle">
© Copyright 2003 Arthur Dent
</div>
</body>
</html>.tt or .html file extension for
page templates (e.g., hello.tt), and no
extension for component templates (e.g.,
header), but this is entirely arbitrary. If you
want to give them an extension (e.g.,
header.ttc), that's fine.<html>
<head>
<title>[% author %]: [% title %]</title>
</head>
<body bgcolor="[% bgcol %]">
<h1>[% title %]</h1> <hr />
<div align="middle">
© Copyright [% year %] [% author %]
</div>
</body>
</html>PROCESS
directive. Example 2-5 shows this in action.[% author = 'Arthur Dent'
bgcol = '#FF6600' # orange
year = 2003
copyr = "Copyright $year $author"
-%]#
orange. A comment starts with the
#
character and continues to the end of
the current line. The comment is ignored by the Template Toolkit, and
processing continues as normal on the next line.# is used as the first character immediately
following the opening [% tag, the Template Toolkit
ignores the entire directive up to the closing $ cd /home/dent $ mkdir web $ cd web $ mkdir src lib html etc bin
# directories src = /home/dent/web/src lib = /home/dent/web/lib dest = /home/dent/web/html # copy images and other binary files copy = \.(png|gif|jpg)$ # ignore CVS, RCS, and Emacs temporary files ignore = \b(CVS|RCS)\b ignore = ^# # misc options verbose recurse
pre_process and
post_process. These allow you to specify one or
more templates that should be automatically
added to the top or bottom of each page template, respectively. This
can be used to add standard headers and footers to a generated page,
but pre- and postprocessed templates may not generate any visible
output at all. For example, we can use a preprocessed template to
configure some variables that we might want defined for use in the
page template or other template components.pre_process = config pre_process = header post_process = footer
[% title = 'Magrethea' -%] <p> Home of the custom-made luxury-planet building industry. </p>
-a option to force ttree to
rebuild all pages in the site to have the changes take effect:$ bin/build -a
title variable isn't set
to any value when the header is processed. It
doesn't get set until the page template is
processed, by which time it's too late for the
header to use it.<p>
The Hitch Hiker's Guide to the Galaxy
has this to say on the subject of
"[% title %]".
</p>
<table border="0">
<tr valign="top">
<td>
<b>[% title %]:</b>
</td>
<td>
[% content %]
</td>
</tr>
</table>title and
content. The value for title
can in this case be copied from template.title,
thereby providing the title set in the META
directive for the page. A value for content will
be set explicitly for the sake of simplicity. These variables can be
set either before the PROCESS directive:[% title = template.title content = 'Mostly harmless' %] [% PROCESS entry %]
PROCESS directive, following the
template name as additional arguments:[% PROCESS entry
title = template.title
content = 'Mostly harmless'
%]title and content variables
are defined globally and can subsequently be used in both the called
template (entry) and the calling template
(earth.tt) after the point of definition.content variable at the end of the template will
generate the value "Mostly
harmless" as set in the earlier
[% INCLUDE entry
title = 'Vogon Poetry'
content = 'Vogon poetry is of course the
third worst in the Universe.
The second worst is that of...
...etc...
...in the destruction of the
planet Earth'
%]Grunthos is reported to have been "disappointed" by the poem's reception.
poem's" must be
escaped by preceding it with a backslash \
character (the apostrophe and single-quote characters are one and the
same for these purposes):[% INCLUDE entry
title = 'Grunthos the Flatulent'
content = 'Grunthos is reported to have
been "disappointed" by the
poem\'s reception.'
%][% INCLUDE entry
title = 'Grunthos the Flatulent'
content = "Grunthos is reported to have
been \"disappointed\" by the
poem's reception."
%]WRAPPER directive.
It works in a similar way to INCLUDE, but uses an
additional END
directive to enclose a block of
template content. The WRAPPER directive uses this
block as the value for the content variable:[% WRAPPER entry
title = 'Grunthos the Flatulent'
%]
Grunthos is reported to have
been "disappointed" by the
poem's reception.
[% END %]<table border="0">
<tr>
<td>
<img src="/images/icon.png" width="4" height="4" />
</td>
<td>
<a href="earth.html">Earth</a>
</td>
</tr>
<tr>
<td>
<img src="/images/icon.png" width="4" height="4" />
</td>
<td>
<a href="magrethea.html">Magrethea</a>
</td>
</tr>
</table><table>
element, containing one <tr> row for each
item, each of which holds two <td> cells,
one to display an icon, the other a link to a particular page. Only
two items are in this simple example, but already we can see how it
gets repetitive very quickly. This suggests that we can modularize
the markup into separate template components.<table> elements and uses a second template,
menuitem, to generate each item.<table border="0">
[%
PROCESS menuitem
text = 'Earth'
link = 'earth.html';
PROCESS menuitem
text = 'Magrethea'
link = 'magrethea.html';
%]
</table>
[% BLOCK menuitem %]
<tr>
<td>
<img src="/images/icon.png" width="4" height="4" />
</td>
<td>
<a href="[% link %]">[% text %]</a>
</td>
</tr>
[% END %]<table> markup into different files. This
would make it harder to maintain and possibly lead to tag mismatch or
other formatting errors.BLOCK directive. The argument following the
BLOCK keyword is a name for the template
component, which can then be used in any PROCESS,
INCLUDE, or WRAPPER directives.
The content of the component follows, and can contain any kind of
Template Toolkit directives up to the corresponding
template variable, and the Date plugin in Example 2-22. As we saw in Chapter 1,
the Template Toolkit also supports lists and hash arrays for complex
data, and allows you to access Perl subroutines and objects.author variable was
used to store the name of the site author, Arthur Dent, for use in
the header and footer
templates. At some later date, we might decide to add a
quote template component that also uses the
author variable. This is shown in Example 2-28.<blockquote> [% quote %] </blockquote> -- [% author %]
INCLUDE to load the template, providing a local
variable value for author:[% INCLUDE quote
author = 'Douglas Adams'
quote = 'I love deadlines. I like the
whooshing sound they make as
they fly by.'
%]author supplied as a parameter to
the INCLUDE directive (Douglas
Adams) remains set as a local variable within the
quote template. It doesn't
affect the global author variable that is defined
in the config (Arthur Dent).author variable is
"reserved"—especially if
it's just one of a large number of such
variables—and to use PROCESS instead of
PROCESS,
WRAPPER, USE, and so on). These
are described in detail in Chapter 4. A full
discussion of filters and plugins is left for Chapter 5 and Chapter 6,
respectively.[% and
%] markers. Everything coming after the
[% and before the following %]
is part of the directive tag. Everything else in the document is
fixed text that is passed through intact.PRE_CHOMP and
POST_CHOMP) and related flags (which
we'll be looking at shortly) tell the Template
Toolkit to remove any extraneous whitespace in the text on either
side of (i.e., before or after) a directive. The
INTERPOLATE option is another example that, when
set (which it isn't by default), causes the text
part of the template to be passed through a second scanning process
to look for any embedded variables, denoted by a $
prefix—e.g., Hello $planet. More on that
later.