s wrapped in C tags,
but when processed as F
, will output only the C from the
F
C.
=head1 Template Variables
=head2 VARIABLES
The C option (or C - they're equivalent) can be used
to specify a hash array of template variables that should be used to
pre-initialise the stash when it is created. These items are ignored
if the C item is defined.
my $template = Template->new({
VARIABLES => {
title => 'A Demo Page',
author => 'Joe Random Hacker',
version => 3.14,
},
};
or
my $template = Template->new({
PRE_DEFINE => {
title => 'A Demo Page',
author => 'Joe Random Hacker',
version => 3.14,
},
};
=head2 CONSTANTS
The C option can be used to specify a hash array of template
variables that are compile-time constants. These variables are
resolved once when the template is compiled, and thus don't require
further resolution at runtime. This results in significantly faster
processing of the compiled templates and can be used for variables that
don't change from one request to the next.
my $template = Template->new({
CONSTANTS => {
title => 'A Demo Page',
author => 'Joe Random Hacker',
version => 3.14,
},
};
=head2 CONSTANT_NAMESPACE
Constant variables are accessed via the C namespace by
default.
[% constants.title %]
The C option can be set to specify an alternate
namespace.
my $template = Template->new({
CONSTANTS => {
title => 'A Demo Page',
# ...etc...
},
CONSTANTS_NAMESPACE => 'const',
};
In this case the constants would then be accessed as:
[% const.title %]
=head2 NAMESPACE
The constant folding mechanism described above is an example of a
namespace handler. Namespace handlers can be defined to provide
alternate parsing mechanisms for variables in different namespaces.
Under the hood, the L module converts a constructor configuration
such as:
my $template = Template->new({
CONSTANTS => {
title => 'A Demo Page',
# ...etc...
},
CONSTANTS_NAMESPACE => 'const',
};
into one like:
my $template = Template->new({
NAMESPACE => {
const => Template:::Namespace::Constants->new({
title => 'A Demo Page',
# ...etc...
}),
},
};
You can use this mechanism to define multiple constant namespaces, or
to install custom handlers of your own.
my $template = Template->new({
NAMESPACE => {
site => Template:::Namespace::Constants->new({
title => "Wardley's Widgets",
version => 2.718,
}),
author => Template:::Namespace::Constants->new({
name => 'Andy Wardley',
email => 'abw@andywardley.com',
}),
voodoo => My::Namespace::Handler->new( ... ),
},
};
Now you have two constant namespaces, for example:
[% site.title %]
[% author.name %]
as well as your own custom namespace handler installed for the 'voodoo'
namespace.
[% voodoo.magic %]
See L
for an example of what a namespace handler looks like on the inside.
=head1 Template Processing Options
The following options are used to specify any additional templates that should
be processed before, after, around or instead of the template passed as the
first argument to the L L method.
These options can be perform various useful tasks such as adding standard
headers or footers to all pages, wrapping page output in other templates,
pre-defining variables or performing initialisation or cleanup tasks,
automatically generating page summary information, navigation elements, and so
on.
The task of processing the template is delegated internally to the
L module which, unsurprisingly, also has a
L method. Any templates defined by the
C option are processed first and any output generated is added to
the output buffer. Then the main template is processed, or if one or more
C templates are defined then they are instead processed in turn. In this
case, one of the C templates is responsible for processing the main
template, by a directive such as:
[% PROCESS $template %]
The output of processing the main template or the C template(s)
is then wrapped in any C templates, if defined. C
templates don't need to worry about explicitly processing the template
because it will have been done for them already. Instead C
templates access the content they are wrapping via the C
variable.
wrapper before
[% content %]
wrapper after
This output generated from processing the main template, and/or any
C or C templates is added to the output buffer. Finally,
any C templates are processed and their output is also
added to the output buffer which is then returned.
If the main template throws an exception during processing then any relevant
template(s) defined via the C option will be processed instead. If
defined and successfully processed, the output from the error template will be
added to the output buffer in place of the template that generated the error
and processing will continue, applying any C and C
templates. If no relevant C option is defined, or if the error occurs
in one of the C, C or C templates, then
the process will terminate immediately and the error will be returned.
=head2 PRE_PROCESS, POST_PROCESS
These values may be set to contain the name(s) of template files
(relative to C) which should be processed immediately
before and/or after each template. These do not get added to
templates processed into a document via directives such as C,
C, C etc.
my $template = Template->new({
PRE_PROCESS => 'header',
POST_PROCESS => 'footer',
};
Multiple templates may be specified as a reference to a list. Each is
processed in the order defined.
my $template = Template->new({
PRE_PROCESS => [ 'config', 'header' ],
POST_PROCESS => 'footer',
};
Alternately, multiple template may be specified as a single string,
delimited by 'C<:>'. This delimiter string can be changed via the
C option.
my $template = Template->new({
PRE_PROCESS => 'config:header',
POST_PROCESS => 'footer',
};
The C and C templates are evaluated in the same
variable context as the main document and may define or update
variables for subsequent use.
config:
[% # set some site-wide variables
bgcolor = '#ffffff'
version = 2.718
%]
header:
[% DEFAULT title = 'My Funky Web Site' %]
[% title %]
footer:
Version [% version %]
The L object representing the main template being processed
is available within C and C templates as the C
variable. Metadata items defined via the C directive may be accessed
accordingly.
$template->process('mydoc.html', $vars);
mydoc.html:
[% META title = 'My Document Title' %]
blah blah blah
...
header:
[% template.title %]
=head2 PROCESS
The C option may be set to contain the name(s) of template files
(relative to C) which should be processed instead of the main
template passed to the L L method.
This can be used to apply consistent wrappers around all templates, similar to
the use of C and C templates.
my $template = Template->new({
PROCESS => 'content',
};
# processes 'content' instead of 'foo.html'
$template->process('foo.html');
A reference to the original template is available in the C
variable. Metadata items can be inspected and the template can be
processed by specifying it as a variable reference (i.e. prefixed by
C<$>) to an C, C or C directive.
content:
[% template.title %]
[% PROCESS $template %]
© Copyright [% template.copyright %]
foo.html:
[% META
title = 'The Foo Page'
author = 'Fred Foo'
copyright = '2000 Fred Foo'
%]
[% template.title %]
Welcome to the Foo Page, blah blah blah
output:
The Foo Page
The Foo Page
Welcome to the Foo Page, blah blah blah
© Copyright 2000 Fred Foo
=head2 WRAPPER
The C option can be used to specify one or more templates which
should be used to wrap around the output of the main page template.
The main template is processed first (or any C template(s)) and
the output generated is then passed as the C variable to the
C template(s) as they are processed.
my $template = Template->new({
WRAPPER => 'wrapper',
};
# process 'foo' then wrap in 'wrapper'
$template->process('foo', { message => 'Hello World!' });
wrapper:
[% content %]
foo:
This is the foo file!
Message: [% message %]
The output generated from this example is:
This is the foo file!
Message: Hello World!
You can specify more than one C template by setting the value to
be a reference to a list of templates. The C templates will be
processed in reverse order with the output of each being passed to the
next (or previous, depending on how you look at it) as the 'content'
variable. It sounds complicated, but the end result is that it just
"Does The Right Thing" to make wrapper templates nest in the order you
specify.
my $template = Template->new({
WRAPPER => [ 'outer', 'inner' ],
};
# process 'foo' then wrap in 'inner', then in 'outer'
$template->process('foo', { message => 'Hello World!' });
outer:
[% content %]
inner:
[% content %]
The output generated is then:
This is the foo file!
Message: Hello World!
One side-effect of the "inside-out" processing of the C
configuration item (and also the C directive) is that any
variables set in the template being wrapped will be visible to the
template doing the wrapping, but not the other way around.
You can use this to good effect in allowing page templates to set
pre-defined values which are then used in the wrapper templates. For
example, our main page template 'foo' might look like this:
foo:
[% page = {
title = 'Foo Page'
subtitle = 'Everything There is to Know About Foo'
author = 'Frank Oliver Octagon'
}
%]
Welcome to the page that tells you everything about foo
blah blah blah...
The C template is processed before the wrapper template meaning
that the C data structure will be defined for use in the wrapper
template.
wrapper:
[% page.title %]
[% page.title %]
[% page.subtitle %]
by [% page.author %]
[% content %]
It achieves the same effect as defining C items which are then
accessed via the C variable (which you are still free to
use within C templates), but gives you more flexibility in
the type and complexity of data that you can define.
=head2 ERROR
The C (or C if you prefer) configuration item can be used to
name a single template or specify a hash array mapping exception types
to templates which should be used for error handling. If an uncaught
exception is raised from within a template then the appropriate error
template will instead be processed.
If specified as a single value then that template will be processed
for all uncaught exceptions.
my $template = Template->new({
ERROR => 'error.html'
});
If the C item is a hash reference the keys are assumed to be
exception types and the relevant template for a given exception will
be selected. A C template may be provided for the general
case. Note that C can be pluralised to C if you find
it more appropriate in this case.
my $template = Template->new({
ERRORS => {
user => 'user/index.html',
dbi => 'error/database',
default => 'error/default',
},
});
In this example, any C exceptions thrown will cause the
F template to be processed, C errors are handled
by F and all others by the F template.
Any C and/or C templates will also be applied
to these error templates.
Note that exception types are hierarchical and a C handler will
catch all C errors (e.g. C, C) if a more
specific handler isn't defined. Be sure to quote any exception types
that contain periods to prevent Perl concatenating them into a single
string (i.e. C is parsed as C<'user'.'passwd'>).
my $template = Template->new({
ERROR => {
'user.login' => 'user/login.html',
'user.passwd' => 'user/badpasswd.html',
'user' => 'user/index.html',
'default' => 'error/default',
},
});
In this example, any template processed by the C<$template> object, or
other templates or code called from within, can raise a C
exception and have the service redirect to the F
template. Similarly, a C exception has a specific
handling template, F, while all other C or
C exceptions cause a redirection to the F page.
All other exception types are handled by F.
Exceptions can be raised in a template using the C directive,
[% THROW user.login 'no user id: please login' %]
or by calling the L method on the
current L object,
$context->throw('user.passwd', 'Incorrect Password');
$context->throw('Incorrect Password'); # type 'undef'
or from Perl code by calling C with a L object,
die (Template::Exception->new('user.denied', 'Invalid User ID'));
or by simply calling L with an error string. This is
automagically caught and converted to an exception of 'C'
type which can then be handled in the usual way.
die "I'm sorry Dave, I can't do that";
Note that the 'C' we're talking about here is a literal string
rather than Perl's C used to represent undefined values.
=head1 Template Runtime Options
=head2 EVAL_PERL
This flag is used to indicate if C and/or C blocks should be
evaluated. It is disabled by default and any C or C blocks
encountered will raise exceptions of type 'C' with the message
'C'. Note however that any C blocks should
always contain valid Perl code, regardless of the C flag. The
parser will fail to compile templates that contain invalid Perl code
in C blocks and will throw a 'C' exception.
When using compiled templates (see
L),
the C has an affect when the template is compiled, and again
when the templates is subsequently processed, possibly in a different
context to the one that compiled it.
If the C is set when a template is compiled, then all C and
C blocks will be included in the compiled template. If the
C option isn't set, then Perl code will be generated which
B throws a 'C' exception with the message 'C' B the compiled template code is run.
Thus, you must have C set if you want your compiled templates
to include C and C blocks.
At some point in the future, using a different invocation of the
Template Toolkit, you may come to process such a pre-compiled
template. Assuming the C option was set at the time the
template was compiled, then the output of any C blocks will be
included in the compiled template and will get executed when the
template is processed. This will happen regardless of the runtime
C status.
Regular C blocks are a little more cautious, however. If the
C flag isn't set for the I context, that is, the
one which is trying to process it, then it will throw the familiar 'C'
exception with the message, 'C'.
Thus you can compile templates to include C blocks, but optionally
disable them when you process them later. Note however that it is
possible for a C block to contain a Perl "C"
block which will always get run regardless of the runtime C
status. Thus, if you set C