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 when compiling templates, it is
assumed that you trust the templates to Do The Right Thing. Otherwise
you must accept the fact that there's no bulletproof way to prevent
any included code from trampling around in the living room of the
runtime environment, making a real nuisance of itself if it really
wants to. If you don't like the idea of such uninvited guests causing
a bother, then you can accept the default and keep C disabled.
=head2 OUTPUT
Default output location or handler. This may be specified as one of:
a file name (relative to C, if defined, or the current
working directory if not specified absolutely); a file handle
(e.g. C or L) opened for writing; a reference to a text
string to which the output is appended (the string isn't cleared); a
reference to a subroutine which is called, passing the output text as
an argument; as a reference to an array, onto which the content will be
Ced; or as a reference to any object that supports the C
method. This latter option includes the C