Mini Shell
Direktori : /usr/local/share/man/man3/ |
|
Current File : //usr/local/share/man/man3/Template::Manual::Config.3pm |
.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Template::Manual::Config 3"
.TH Template::Manual::Config 3 "2024-06-21" "perl v5.32.1" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Template::Manual::Config \- Configuration options
.SH "Template Style and Parsing Options"
.IX Header "Template Style and Parsing Options"
.SS "\s-1ENCODING\s0"
.IX Subsection "ENCODING"
The \f(CW\*(C`ENCODING\*(C'\fR option specifies the template files' character encoding:
.PP
.Vb 3
\& my $template = Template\->new({
\& ENCODING => \*(Aqutf8\*(Aq,
\& });
.Ve
.PP
A template which starts with a Unicode byte order mark (\s-1BOM\s0) will have its
encoding detected automatically.
.SS "\s-1START_TAG, END_TAG\s0"
.IX Subsection "START_TAG, END_TAG"
The \f(CW\*(C`START_TAG\*(C'\fR and \f(CW\*(C`END_TAG\*(C'\fR options are used to specify character
sequences or regular expressions that mark the start and end of inline
template directives. The default values for \f(CW\*(C`START_TAG\*(C'\fR and \f(CW\*(C`END_TAG\*(C'\fR are
\&'\f(CW\*(C`[%\*(C'\fR' and '\f(CW\*(C`%]\*(C'\fR' respectively, giving us the familiar directive style:
.PP
.Vb 1
\& [% example %]
.Ve
.PP
Any Perl regex characters can be used and therefore should be escaped
(or use the Perl \f(CW\*(C`quotemeta\*(C'\fR function) if they are intended to
represent literal characters.
.PP
.Vb 4
\& my $template = Template\->new({
\& START_TAG => quotemeta(\*(Aq<+\*(Aq),
\& END_TAG => quotemeta(\*(Aq+>\*(Aq),
\& });
.Ve
.PP
Example:
.PP
.Vb 1
\& <+ INCLUDE foobar +>
.Ve
.PP
The \f(CW\*(C`TAGS\*(C'\fR directive can also be used to set the \f(CW\*(C`START_TAG\*(C'\fR and \f(CW\*(C`END_TAG\*(C'\fR values
on a per-template file basis.
.PP
.Vb 1
\& [% TAGS <+ +> %]
.Ve
.SS "\s-1OUTLINE_TAG\s0"
.IX Subsection "OUTLINE_TAG"
The \f(CW\*(C`OUTLINE_TAG\*(C'\fR option can be used to enable single-line \*(L"outline\*(R" directives.
.PP
.Vb 3
\& my $template = Template\->new({
\& OUTLINE_TAG => \*(Aq%%\*(Aq,
\& });
.Ve
.PP
This allows you to use both inline and outline tags like so:
.PP
.Vb 3
\& %% IF user
\& Hello [% user.name %]
\& %% END
.Ve
.PP
The \f(CW\*(C`OUTLINE_TAG\*(C'\fR string (or regex) must appear at the start of a line. The
directive continues until the end of the line. The newline character at the
end of the line is considered to be the invisible end-of-directive marker and
is removed.
.SS "\s-1TAG_STYLE\s0"
.IX Subsection "TAG_STYLE"
The \f(CW\*(C`TAG_STYLE\*(C'\fR option can be used to set both \f(CW\*(C`START_TAG\*(C'\fR and \f(CW\*(C`END_TAG\*(C'\fR
according to pre-defined tag styles.
.PP
.Vb 3
\& my $template = Template\->new({
\& TAG_STYLE => \*(Aqstar\*(Aq,
\& });
.Ve
.PP
Available styles are:
.PP
.Vb 8
\& template [% ... %] (default)
\& template1 [% ... %] or %% ... %% (TT version 1)
\& metatext %% ... %% (Text::MetaText)
\& star [* ... *] (TT alternate)
\& php <? ... ?> (PHP)
\& asp <% ... %> (ASP)
\& mason <% ... > (HTML::Mason)
\& html <!\-\- ... \-\-> (HTML comments)
.Ve
.PP
The \f(CW\*(C`outline\*(C'\fR style uses the default markers for \f(CW\*(C`START_TAG\*(C'\fR and \f(CW\*(C`END_TAG\*(C'\fR
(\f(CW\*(C`[%\*(C'\fR and \f(CW\*(C`%]\*(C'\fR respectively) and additionally defines \f(CW\*(C`OUTLINE_TAG\*(C'\fR to
be \f(CW\*(C`%%\*(C'\fR.
.PP
.Vb 3
\& my $template = Template\->new({
\& TAG_STYLE => \*(Aqoutline\*(Aq,
\& });
.Ve
.PP
This allows you to use both inline and outline tags like so:
.PP
.Vb 3
\& %% IF user
\& Hello [% user.name %]
\& %% END
.Ve
.PP
Any values specified for \f(CW\*(C`START_TAG\*(C'\fR, \f(CW\*(C`END_TAG\*(C'\fR and/or \f(CW\*(C`OUTLINE_TAG\*(C'\fR
will override those defined by a \f(CW\*(C`TAG_STYLE\*(C'\fR.
.PP
The \f(CW\*(C`TAGS\*(C'\fR directive may also be used to set a \f(CW\*(C`TAG_STYLE\*(C'\fR
.PP
.Vb 2
\& [% TAGS html %]
\& <!\-\- INCLUDE header \-\->
.Ve
.SS "\s-1PRE_CHOMP, POST_CHOMP\s0"
.IX Subsection "PRE_CHOMP, POST_CHOMP"
Anything outside a directive tag is considered plain text and is
generally passed through unaltered (but see the \s-1INTERPOLATE\s0 option).
This includes all whitespace and newlines characters surrounding
directive tags. Directives that don't generate any output will leave
gaps in the output document.
.PP
Example:
.PP
.Vb 3
\& Foo
\& [% a = 10 %]
\& Bar
.Ve
.PP
Output:
.PP
.Vb 1
\& Foo
\&
\& Bar
.Ve
.PP
The \f(CW\*(C`PRE_CHOMP\*(C'\fR and \f(CW\*(C`POST_CHOMP\*(C'\fR options can help to clean up some of this
extraneous whitespace. Both are disabled by default.
.PP
.Vb 4
\& my $template = Template\->new({
\& PRE_CHOMP => 1,
\& POST_CHOMP => 1,
\& });
.Ve
.PP
With \f(CW\*(C`PRE_CHOMP\*(C'\fR set to \f(CW1\fR, the newline and whitespace preceding a directive
at the start of a line will be deleted. This has the effect of
concatenating a line that starts with a directive onto the end of the
previous line.
.PP
.Vb 9
\& Foo <\-\-\-\-\-\-\-\-\-\-.
\& |
\& ,\-\-\-(PRE_CHOMP)\-\-\-\-\*(Aq
\& |
\& \`\-\- [% a = 10 %] \-\-.
\& |
\& ,\-\-\-(POST_CHOMP)\-\-\-\*(Aq
\& |
\& \`\-> Bar
.Ve
.PP
With \f(CW\*(C`POST_CHOMP\*(C'\fR set to \f(CW1\fR, any whitespace after a directive up to and
including the newline will be deleted. This has the effect of joining
a line that ends with a directive onto the start of the next line.
.PP
If \f(CW\*(C`PRE_CHOMP\*(C'\fR or \f(CW\*(C`POST_CHOMP\*(C'\fR is set to \f(CW2\fR, all whitespace including any
number of newline will be removed and replaced with a single space.
This is useful for \s-1HTML,\s0 where (usually) a contiguous block of
whitespace is rendered the same as a single space.
.PP
With \f(CW\*(C`PRE_CHOMP\*(C'\fR or \f(CW\*(C`POST_CHOMP\*(C'\fR set to \f(CW3\fR, all adjacent whitespace
(including newlines) will be removed entirely.
.PP
These values are defined as \f(CW\*(C`CHOMP_NONE\*(C'\fR, \f(CW\*(C`CHOMP_ONE\*(C'\fR, \f(CW\*(C`CHOMP_COLLAPSE\*(C'\fR and
\&\f(CW\*(C`CHOMP_GREEDY\*(C'\fR constants in the Template::Constants module. \f(CW\*(C`CHOMP_ALL\*(C'\fR
is also defined as an alias for \f(CW\*(C`CHOMP_ONE\*(C'\fR to provide backwards
compatibility with earlier version of the Template Toolkit.
.PP
Additionally the chomp tag modifiers listed below may also be used for
the \f(CW\*(C`PRE_CHOMP\*(C'\fR and \f(CW\*(C`POST_CHOMP\*(C'\fR configuration.
.PP
.Vb 4
\& my $template = Template\->new({
\& PRE_CHOMP => \*(Aq~\*(Aq,
\& POST_CHOMP => \*(Aq\-\*(Aq,
\& });
.Ve
.PP
\&\f(CW\*(C`PRE_CHOMP\*(C'\fR and \f(CW\*(C`POST_CHOMP\*(C'\fR can be activated for individual directives by
placing a '\f(CW\*(C`\-\*(C'\fR' immediately at the start and/or end of the directive.
.PP
.Vb 3
\& [% FOREACH user IN userlist %]
\& [%\- user \-%]
\& [% END %]
.Ve
.PP
This has the same effect as \f(CW\*(C`CHOMP_ONE\*(C'\fR in removing all whitespace
before or after the directive up to and including the newline. The
template will be processed as if written:
.PP
.Vb 1
\& [% FOREACH user IN userlist %][% user %][% END %]
.Ve
.PP
To remove all whitespace including any number of newlines, use the ('\f(CW\*(C`~\*(C'\fR')
tilde character instead.
.PP
.Vb 1
\& [% FOREACH user IN userlist %]
\&
\& [%~ user ~%]
\&
\& [% END %]
.Ve
.PP
To collapse all whitespace to a single space, use the '\f(CW\*(C`=\*(C'\fR' equals sign character.
.PP
.Vb 1
\& [% FOREACH user IN userlist %]
\&
\& [%= user =%]
\&
\& [% END %]
.Ve
.PP
Here the template is processed as if written:
.PP
.Vb 1
\& [% FOREACH user IN userlist %] [% user %] [% END %]
.Ve
.PP
If you have \f(CW\*(C`PRE_CHOMP\*(C'\fR or \f(CW\*(C`POST_CHOMP\*(C'\fR set as configuration options then
you can use the '\f(CW\*(C`+\*(C'\fR' plus sign to disable any chomping options (i.e. leave the
whitespace intact) on a per-directive basis.
.PP
.Vb 3
\& [% FOREACH user IN userlist %]
\& User: [% user +%]
\& [% END %]
.Ve
.PP
With \f(CW\*(C`POST_CHOMP\*(C'\fR set to \f(CW\*(C`CHOMP_ONE\*(C'\fR, the above example would be parsed as
if written:
.PP
.Vb 2
\& [% FOREACH user IN userlist %]User: [% user %]
\& [% END %]
.Ve
.PP
For reference, the \f(CW\*(C`PRE_CHOMP\*(C'\fR and \f(CW\*(C`POST_CHOMP\*(C'\fR configuration options may be
set to any of the following:
.PP
.Vb 6
\& Constant Value Tag Modifier
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& CHOMP_NONE 0 +
\& CHOMP_ONE 1 \-
\& CHOMP_COLLAPSE 2 =
\& CHOMP_GREEDY 3 ~
.Ve
.SS "\s-1TRIM\s0"
.IX Subsection "TRIM"
The \f(CW\*(C`TRIM\*(C'\fR option can be set to have any leading and trailing whitespace
automatically removed from the output of all template files and \f(CW\*(C`BLOCK\*(C'\fRs.
.PP
By example, the following \f(CW\*(C`BLOCK\*(C'\fR definition
.PP
.Vb 3
\& [% BLOCK foo %]
\& Line 1 of foo
\& [% END %]
.Ve
.PP
will be processed is as "\f(CW\*(C`\enLine 1 of foo\en\*(C'\fR". When \f(CW\*(C`INCLUDE\*(C'\fRd, the surrounding
newlines will also be introduced.
.PP
.Vb 3
\& before
\& [% INCLUDE foo %]
\& after
.Ve
.PP
Generated output:
.PP
.Vb 1
\& before
\&
\& Line 1 of foo
\&
\& after
.Ve
.PP
With the \f(CW\*(C`TRIM\*(C'\fR option set to any true value, the leading and trailing
newlines (which count as whitespace) will be removed from the output
of the \f(CW\*(C`BLOCK\*(C'\fR.
.PP
.Vb 3
\& before
\& Line 1 of foo
\& after
.Ve
.PP
The \f(CW\*(C`TRIM\*(C'\fR option is disabled (\f(CW0\fR) by default.
.SS "\s-1INTERPOLATE\s0"
.IX Subsection "INTERPOLATE"
The \f(CW\*(C`INTERPOLATE\*(C'\fR flag, when set to any true value will cause variable
references in plain text (i.e. not surrounded by \f(CW\*(C`START_TAG\*(C'\fR and \f(CW\*(C`END_TAG\*(C'\fR)
to be recognised and interpolated accordingly.
.PP
.Vb 3
\& my $template = Template\->new({
\& INTERPOLATE => 1,
\& });
.Ve
.PP
Variables should be prefixed by a '\f(CW\*(C`$\*(C'\fR' dollar sign to identify them.
Curly braces '\f(CW\*(C`{\*(C'\fR' and '\f(CW\*(C`}\*(C'\fR'
can be used in the familiar Perl/shell style to explicitly scope the
variable name where required.
.PP
.Vb 4
\& # INTERPOLATE => 0
\& <a href="http://[% server %]/[% help %]">
\& <img src="[% images %]/help.gif"></a>
\& [% myorg.name %]
\&
\& # INTERPOLATE => 1
\& <a href="http://$server/$help">
\& <img src="$images/help.gif"></a>
\& $myorg.name
\&
\& # explicit scoping with { }
\& <img src="$images/${icon.next}.gif">
.Ve
.PP
Note that a limitation in Perl's regex engine restricts the maximum length
of an interpolated template to around 32 kilobytes or possibly less. Files
that exceed this limit in size will typically cause Perl to dump core with
a segmentation fault. If you routinely process templates of this size
then you should disable \f(CW\*(C`INTERPOLATE\*(C'\fR or split the templates in several
smaller files or blocks which can then be joined backed together via
\&\f(CW\*(C`PROCESS\*(C'\fR or \f(CW\*(C`INCLUDE\*(C'\fR.
.SS "\s-1ANYCASE\s0"
.IX Subsection "ANYCASE"
By default, directive keywords should be expressed in \s-1UPPER CASE.\s0 The
\&\f(CW\*(C`ANYCASE\*(C'\fR option can be set to allow directive keywords to be specified
in any case.
.PP
.Vb 4
\& # ANYCASE => 0 (default)
\& [% INCLUDE foobar %] # OK
\& [% include foobar %] # ERROR
\& [% include = 10 %] # OK, \*(Aqinclude\*(Aq is a variable
\&
\& # ANYCASE => 1
\& [% INCLUDE foobar %] # OK
\& [% include foobar %] # OK
\& [% include = 10 %] # ERROR, \*(Aqinclude\*(Aq is reserved word
.Ve
.PP
One side-effect of enabling \f(CW\*(C`ANYCASE\*(C'\fR is that you cannot use a variable
of the same name as a reserved word, regardless of case. The reserved
words are currently:
.PP
.Vb 5
\& GET CALL SET DEFAULT INSERT INCLUDE PROCESS WRAPPER
\& IF UNLESS ELSE ELSIF FOR FOREACH WHILE SWITCH CASE
\& USE PLUGIN FILTER MACRO PERL RAWPERL BLOCK META
\& TRY THROW CATCH FINAL NEXT LAST BREAK RETURN STOP
\& CLEAR TO STEP AND OR NOT MOD DIV END
.Ve
.PP
The only lower case reserved words that cannot be used for variables,
regardless of the \f(CW\*(C`ANYCASE\*(C'\fR option, are the operators:
.PP
.Vb 1
\& and or not mod div
.Ve
.SH "Template Files and Blocks"
.IX Header "Template Files and Blocks"
.SS "\s-1INCLUDE_PATH\s0"
.IX Subsection "INCLUDE_PATH"
The \f(CW\*(C`INCLUDE_PATH\*(C'\fR is used to specify one or more directories in which
template files are located. When a template is requested that isn't
defined locally as a \f(CW\*(C`BLOCK\*(C'\fR, each of the \f(CW\*(C`INCLUDE_PATH\*(C'\fR directories is
searched in turn to locate the template file. Multiple directories
can be specified as a reference to a list or as a single string where
each directory is delimited by the '\f(CW\*(C`:\*(C'\fR' colon character.
.PP
.Vb 3
\& my $template = Template\->new({
\& INCLUDE_PATH => \*(Aq/usr/local/templates\*(Aq,
\& });
\&
\& my $template = Template\->new({
\& INCLUDE_PATH => \*(Aq/usr/local/templates:/tmp/my/templates\*(Aq,
\& });
\&
\& my $template = Template\->new({
\& INCLUDE_PATH => [ \*(Aq/usr/local/templates\*(Aq,
\& \*(Aq/tmp/my/templates\*(Aq ],
\& });
.Ve
.PP
On Win32 systems, a little extra magic is invoked, ignoring delimiters
that have '\f(CW\*(C`:\*(C'\fR' colon followed by a '\f(CW\*(C`/\*(C'\fR' slash or '\f(CW\*(C`\e\*(C'\fR' blackslash.
This avoids confusion when using directory names like '\f(CW\*(C`C:\eBlah Blah\*(C'\fR'.
.PP
When specified as a list, the \f(CW\*(C`INCLUDE_PATH\*(C'\fR path can contain elements
which dynamically generate a list of \f(CW\*(C`INCLUDE_PATH\*(C'\fR directories. These
generator elements can be specified as a reference to a subroutine or
an object which implements a \f(CW\*(C`paths()\*(C'\fR method.
.PP
.Vb 5
\& my $template = Template\->new({
\& INCLUDE_PATH => [ \*(Aq/usr/local/templates\*(Aq,
\& \e&incpath_generator,
\& My::IncPath::Generator\->new( ... ) ],
\& });
.Ve
.PP
Each time a template is requested and the \f(CW\*(C`INCLUDE_PATH\*(C'\fR examined, the
subroutine or object method will be called. A reference to a list of
directories should be returned. Generator subroutines should report
errors using \f(CW\*(C`die()\*(C'\fR. Generator objects should return undef and make an
error available via its \f(CW\*(C`error()\*(C'\fR method.
.PP
For example:
.PP
.Vb 2
\& sub incpath_generator {
\& # ...some code...
\&
\& if ($all_is_well) {
\& return \e@list_of_directories;
\& }
\& else {
\& die "cannot generate INCLUDE_PATH...\en";
\& }
\& }
.Ve
.PP
or:
.PP
.Vb 1
\& package My::IncPath::Generator;
\&
\& # Template::Base (or Class::Base) provides error() method
\& use Template::Base;
\& use base qw( Template::Base );
\&
\& sub paths {
\& my $self = shift;
\&
\& # ...some code...
\&
\& if ($all_is_well) {
\& return \e@list_of_directories;
\& }
\& else {
\& return $self\->error("cannot generate INCLUDE_PATH...\en");
\& }
\& }
\&
\& 1;
.Ve
.SS "\s-1DELIMITER\s0"
.IX Subsection "DELIMITER"
Used to provide an alternative delimiter character sequence for
separating paths specified in the \f(CW\*(C`INCLUDE_PATH\*(C'\fR. The default
value for \f(CW\*(C`DELIMITER\*(C'\fR is the '\f(CW\*(C`:\*(C'\fR' colon character.
.PP
.Vb 4
\& my $template = Template\->new({
\& DELIMITER => \*(Aq; \*(Aq,
\& INCLUDE_PATH => \*(AqC:/HERE/NOW; D:/THERE/THEN\*(Aq,
\& });
.Ve
.PP
On Win32 systems, the default delimiter is a little more intelligent,
splitting paths only on '\f(CW\*(C`:\*(C'\fR' colon characters that aren't followed by a
\&'\f(CW\*(C`/\*(C'\fR' slash character.
This means that the following should work as planned, splitting the
\&\f(CW\*(C`INCLUDE_PATH\*(C'\fR into 2 separate directories, \f(CW\*(C`C:/foo\*(C'\fR and \f(CW\*(C`C:/bar\*(C'\fR.
.PP
.Vb 4
\& # on Win32 only
\& my $template = Template\->new({
\& INCLUDE_PATH => \*(AqC:/Foo:C:/Bar\*(Aq
\& });
.Ve
.PP
However, if you're using Win32 then it's recommended that you
explicitly set the \f(CW\*(C`DELIMITER\*(C'\fR character to something else (e.g. '\f(CW\*(C`;\*(C'\fR' semicolon)
rather than rely on this subtle magic.
.SS "\s-1ABSOLUTE\s0"
.IX Subsection "ABSOLUTE"
The \f(CW\*(C`ABSOLUTE\*(C'\fR flag is used to indicate if templates specified with
absolute filenames (e.g. '\f(CW\*(C`/foo/bar\*(C'\fR') should be processed. It is
disabled by default and any attempt to load a template by such a
name will cause a '\f(CW\*(C`file\*(C'\fR' exception to be raised.
.PP
.Vb 3
\& my $template = Template\->new({
\& ABSOLUTE => 1,
\& });
\&
\& # this is why it\*(Aqs disabled by default
\& [% INSERT /etc/passwd %]
.Ve
.PP
On Win32 systems, the regular expression for matching absolute
pathnames is tweaked slightly to also detect filenames that start
with a driver letter and colon, such as:
.PP
.Vb 1
\& C:/Foo/Bar
.Ve
.SS "\s-1RELATIVE\s0"
.IX Subsection "RELATIVE"
The \f(CW\*(C`RELATIVE\*(C'\fR flag is used to indicate if templates specified with
filenames relative to the current directory (e.g. '\f(CW\*(C`./foo/bar\*(C'\fR' or
\&'\f(CW\*(C`../../some/where/else\*(C'\fR') should be loaded. It is also disabled by
default, and will raise a '\f(CW\*(C`file\*(C'\fR' error if such template names are
encountered.
.PP
.Vb 3
\& my $template = Template\->new({
\& RELATIVE => 1,
\& });
\&
\& [% INCLUDE ../logs/error.log %]
.Ve
.SS "\s-1DEFAULT\s0"
.IX Subsection "DEFAULT"
The \f(CW\*(C`DEFAULT\*(C'\fR option can be used to specify a default template which should
be used whenever a specified template can't be found in the \f(CW\*(C`INCLUDE_PATH\*(C'\fR.
.PP
.Vb 3
\& my $template = Template\->new({
\& DEFAULT => \*(Aqnotfound.html\*(Aq,
\& });
.Ve
.PP
If a non-existent template is requested through the Template
\&\fBprocess()\fR method, or by an \f(CW\*(C`INCLUDE\*(C'\fR, \f(CW\*(C`PROCESS\*(C'\fR or
\&\f(CW\*(C`WRAPPER\*(C'\fR directive, then the \f(CW\*(C`DEFAULT\*(C'\fR template will instead be processed, if
defined. Note that the \f(CW\*(C`DEFAULT\*(C'\fR template is not used when templates are
specified with absolute or relative filenames, or as a reference to a input
file handle or text string.
.SS "\s-1BLOCKS\s0"
.IX Subsection "BLOCKS"
The \f(CW\*(C`BLOCKS\*(C'\fR option can be used to pre-define a default set of template
blocks. These should be specified as a reference to a hash array
mapping template names to template text, subroutines or Template::Document
objects.
.PP
.Vb 7
\& my $template = Template\->new({
\& BLOCKS => {
\& header => \*(AqThe Header. [% title %]\*(Aq,
\& footer => sub { return $some_output_text },
\& another => Template::Document\->new({ ... }),
\& },
\& });
.Ve
.SS "\s-1VIEWS\s0"
.IX Subsection "VIEWS"
The \s-1VIEWS\s0 option can be used to define one or more Template::View
objects. They can be specified as a reference to a hash array or list
reference.
.PP
.Vb 5
\& my $template = Template\->new({
\& VIEWS => {
\& my_view => { prefix => \*(Aqmy_templates/\*(Aq },
\& },
\& });
.Ve
.PP
Be aware of the fact that Perl's hash array are unordered, so if you want to
specify multiple views of which one or more are based on other views, then
you should use a list reference to preserve the order of definition.
.PP
.Vb 7
\& my $template = Template\->new({
\& VIEWS => [
\& bottom => { prefix => \*(Aqbottom/\*(Aq },
\& middle => { prefix => \*(Aqmiddle/\*(Aq, base => \*(Aqbottom\*(Aq },
\& top => { prefix => \*(Aqtop/\*(Aq, base => \*(Aqmiddle\*(Aq },
\& ],
\& });
.Ve
.SS "\s-1AUTO_RESET\s0"
.IX Subsection "AUTO_RESET"
The \f(CW\*(C`AUTO_RESET\*(C'\fR option is set by default and causes the local \f(CW\*(C`BLOCKS\*(C'\fR
cache for the Template::Context object to be reset on each call to the
Template \fBprocess()\fR method. This ensures that any \f(CW\*(C`BLOCK\*(C'\fRs
defined within a template will only persist until that template is finished
processing. This prevents \f(CW\*(C`BLOCK\*(C'\fRs defined in one processing request from
interfering with other independent requests subsequently processed by the same
context object.
.PP
The \f(CW\*(C`BLOCKS\*(C'\fR item may be used to specify a default set of block definitions
for the Template::Context object. Subsequent \f(CW\*(C`BLOCK\*(C'\fR definitions in
templates will over-ride these but they will be reinstated on each reset if
\&\f(CW\*(C`AUTO_RESET\*(C'\fR is enabled (default), or if the Template::Context
\&\fBreset()\fR method is called.
.SS "\s-1RECURSION\s0"
.IX Subsection "RECURSION"
The template processor will raise a file exception if it detects
direct or indirect recursion into a template. Setting this option to
any true value will allow templates to include each other recursively.
.SH "Template Variables"
.IX Header "Template Variables"
.SS "\s-1VARIABLES\s0"
.IX Subsection "VARIABLES"
The \f(CW\*(C`VARIABLES\*(C'\fR option (or \f(CW\*(C`PRE_DEFINE\*(C'\fR \- 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 \f(CW\*(C`STASH\*(C'\fR item is defined.
.PP
.Vb 7
\& my $template = Template\->new({
\& VARIABLES => {
\& title => \*(AqA Demo Page\*(Aq,
\& author => \*(AqJoe Random Hacker\*(Aq,
\& version => 3.14,
\& },
\& };
.Ve
.PP
or
.PP
.Vb 7
\& my $template = Template\->new({
\& PRE_DEFINE => {
\& title => \*(AqA Demo Page\*(Aq,
\& author => \*(AqJoe Random Hacker\*(Aq,
\& version => 3.14,
\& },
\& };
.Ve
.SS "\s-1CONSTANTS\s0"
.IX Subsection "CONSTANTS"
The \f(CW\*(C`CONSTANTS\*(C'\fR 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.
.PP
.Vb 7
\& my $template = Template\->new({
\& CONSTANTS => {
\& title => \*(AqA Demo Page\*(Aq,
\& author => \*(AqJoe Random Hacker\*(Aq,
\& version => 3.14,
\& },
\& };
.Ve
.SS "\s-1CONSTANT_NAMESPACE\s0"
.IX Subsection "CONSTANT_NAMESPACE"
Constant variables are accessed via the \f(CW\*(C`constants\*(C'\fR namespace by
default.
.PP
.Vb 1
\& [% constants.title %]
.Ve
.PP
The \f(CW\*(C`CONSTANTS_NAMESPACE\*(C'\fR option can be set to specify an alternate
namespace.
.PP
.Vb 7
\& my $template = Template\->new({
\& CONSTANTS => {
\& title => \*(AqA Demo Page\*(Aq,
\& # ...etc...
\& },
\& CONSTANTS_NAMESPACE => \*(Aqconst\*(Aq,
\& };
.Ve
.PP
In this case the constants would then be accessed as:
.PP
.Vb 1
\& [% const.title %]
.Ve
.SS "\s-1NAMESPACE\s0"
.IX Subsection "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.
.PP
Under the hood, the Template module converts a constructor configuration
such as:
.PP
.Vb 7
\& my $template = Template\->new({
\& CONSTANTS => {
\& title => \*(AqA Demo Page\*(Aq,
\& # ...etc...
\& },
\& CONSTANTS_NAMESPACE => \*(Aqconst\*(Aq,
\& };
.Ve
.PP
into one like:
.PP
.Vb 8
\& my $template = Template\->new({
\& NAMESPACE => {
\& const => Template:::Namespace::Constants\->new({
\& title => \*(AqA Demo Page\*(Aq,
\& # ...etc...
\& }),
\& },
\& };
.Ve
.PP
You can use this mechanism to define multiple constant namespaces, or
to install custom handlers of your own.
.PP
.Vb 10
\& my $template = Template\->new({
\& NAMESPACE => {
\& site => Template:::Namespace::Constants\->new({
\& title => "Wardley\*(Aqs Widgets",
\& version => 2.718,
\& }),
\& author => Template:::Namespace::Constants\->new({
\& name => \*(AqAndy Wardley\*(Aq,
\& email => \*(Aqabw@andywardley.com\*(Aq,
\& }),
\& voodoo => My::Namespace::Handler\->new( ... ),
\& },
\& };
.Ve
.PP
Now you have two constant namespaces, for example:
.PP
.Vb 2
\& [% site.title %]
\& [% author.name %]
.Ve
.PP
as well as your own custom namespace handler installed for the 'voodoo'
namespace.
.PP
.Vb 1
\& [% voodoo.magic %]
.Ve
.PP
See Template::Namespace::Constants
for an example of what a namespace handler looks like on the inside.
.SH "Template Processing Options"
.IX Header "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 Template \fBprocess()\fR 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.
.PP
The task of processing the template is delegated internally to the
Template::Service module which, unsurprisingly, also has a
\&\fBprocess()\fR method. Any templates defined by the
\&\f(CW\*(C`PRE_PROCESS\*(C'\fR 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
\&\f(CW\*(C`PROCESS\*(C'\fR templates are defined then they are instead processed in turn. In this
case, one of the \f(CW\*(C`PROCESS\*(C'\fR templates is responsible for processing the main
template, by a directive such as:
.PP
.Vb 1
\& [% PROCESS $template %]
.Ve
.PP
The output of processing the main template or the \f(CW\*(C`PROCESS\*(C'\fR template(s)
is then wrapped in any \f(CW\*(C`WRAPPER\*(C'\fR templates, if defined. \f(CW\*(C`WRAPPER\*(C'\fR
templates don't need to worry about explicitly processing the template
because it will have been done for them already. Instead \f(CW\*(C`WRAPPER\*(C'\fR
templates access the content they are wrapping via the \f(CW\*(C`content\*(C'\fR
variable.
.PP
.Vb 3
\& wrapper before
\& [% content %]
\& wrapper after
.Ve
.PP
This output generated from processing the main template, and/or any
\&\f(CW\*(C`PROCESS\*(C'\fR or \f(CW\*(C`WRAPPER\*(C'\fR templates is added to the output buffer. Finally,
any \f(CW\*(C`POST_PROCESS\*(C'\fR templates are processed and their output is also
added to the output buffer which is then returned.
.PP
If the main template throws an exception during processing then any relevant
template(s) defined via the \f(CW\*(C`ERROR\*(C'\fR 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 \f(CW\*(C`WRAPPER\*(C'\fR and \f(CW\*(C`POST_PROCESS\*(C'\fR
templates. If no relevant \f(CW\*(C`ERROR\*(C'\fR option is defined, or if the error occurs
in one of the \f(CW\*(C`PRE_PROCESS\*(C'\fR, \f(CW\*(C`WRAPPER\*(C'\fR or \f(CW\*(C`POST_PROCESS\*(C'\fR templates, then
the process will terminate immediately and the error will be returned.
.SS "\s-1PRE_PROCESS, POST_PROCESS\s0"
.IX Subsection "PRE_PROCESS, POST_PROCESS"
These values may be set to contain the name(s) of template files
(relative to \f(CW\*(C`INCLUDE_PATH\*(C'\fR) 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 \f(CW\*(C`INCLUDE\*(C'\fR,
\&\f(CW\*(C`PROCESS\*(C'\fR, \f(CW\*(C`WRAPPER\*(C'\fR etc.
.PP
.Vb 4
\& my $template = Template\->new({
\& PRE_PROCESS => \*(Aqheader\*(Aq,
\& POST_PROCESS => \*(Aqfooter\*(Aq,
\& };
.Ve
.PP
Multiple templates may be specified as a reference to a list. Each is
processed in the order defined.
.PP
.Vb 4
\& my $template = Template\->new({
\& PRE_PROCESS => [ \*(Aqconfig\*(Aq, \*(Aqheader\*(Aq ],
\& POST_PROCESS => \*(Aqfooter\*(Aq,
\& };
.Ve
.PP
Alternately, multiple template may be specified as a single string,
delimited by '\f(CW\*(C`:\*(C'\fR'. This delimiter string can be changed via the
\&\f(CW\*(C`DELIMITER\*(C'\fR option.
.PP
.Vb 4
\& my $template = Template\->new({
\& PRE_PROCESS => \*(Aqconfig:header\*(Aq,
\& POST_PROCESS => \*(Aqfooter\*(Aq,
\& };
.Ve
.PP
The \f(CW\*(C`PRE_PROCESS\*(C'\fR and \f(CW\*(C`POST_PROCESS\*(C'\fR templates are evaluated in the same
variable context as the main document and may define or update
variables for subsequent use.
.PP
config:
.PP
.Vb 4
\& [% # set some site\-wide variables
\& bgcolor = \*(Aq#ffffff\*(Aq
\& version = 2.718
\& %]
.Ve
.PP
header:
.PP
.Vb 6
\& [% DEFAULT title = \*(AqMy Funky Web Site\*(Aq %]
\& <html>
\& <head>
\& <title>[% title %]</title>
\& </head>
\& <body bgcolor="[% bgcolor %]">
.Ve
.PP
footer:
.PP
.Vb 4
\& <hr>
\& Version [% version %]
\& </body>
\& </html>
.Ve
.PP
The Template::Document object representing the main template being processed
is available within \f(CW\*(C`PRE_PROCESS\*(C'\fR and \f(CW\*(C`POST_PROCESS\*(C'\fR templates as the \f(CW\*(C`template\*(C'\fR
variable. Metadata items defined via the \f(CW\*(C`META\*(C'\fR directive may be accessed
accordingly.
.PP
.Vb 1
\& $template\->process(\*(Aqmydoc.html\*(Aq, $vars);
.Ve
.PP
mydoc.html:
.PP
.Vb 3
\& [% META title = \*(AqMy Document Title\*(Aq %]
\& blah blah blah
\& ...
.Ve
.PP
header:
.PP
.Vb 5
\& <html>
\& <head>
\& <title>[% template.title %]</title>
\& </head>
\& <body bgcolor="[% bgcolor %]">
.Ve
.SS "\s-1PROCESS\s0"
.IX Subsection "PROCESS"
The \f(CW\*(C`PROCESS\*(C'\fR option may be set to contain the name(s) of template files
(relative to \f(CW\*(C`INCLUDE_PATH\*(C'\fR) which should be processed instead of the main
template passed to the Template \fBprocess()\fR method.
This can be used to apply consistent wrappers around all templates, similar to
the use of \f(CW\*(C`PRE_PROCESS\*(C'\fR and \f(CW\*(C`POST_PROCESS\*(C'\fR templates.
.PP
.Vb 3
\& my $template = Template\->new({
\& PROCESS => \*(Aqcontent\*(Aq,
\& };
\&
\& # processes \*(Aqcontent\*(Aq instead of \*(Aqfoo.html\*(Aq
\& $template\->process(\*(Aqfoo.html\*(Aq);
.Ve
.PP
A reference to the original template is available in the \f(CW\*(C`template\*(C'\fR
variable. Metadata items can be inspected and the template can be
processed by specifying it as a variable reference (i.e. prefixed by
\&\f(CW\*(C`$\*(C'\fR) to an \f(CW\*(C`INCLUDE\*(C'\fR, \f(CW\*(C`PROCESS\*(C'\fR or \f(CW\*(C`WRAPPER\*(C'\fR directive.
.PP
content:
.PP
.Vb 12
\& <html>
\& <head>
\& <title>[% template.title %]</title>
\& </head>
\& <body>
\& <!\-\- begin content \-\->
\& [% PROCESS $template %]
\& <!\-\- end content \-\->
\& <hr>
\& © Copyright [% template.copyright %]
\& </body>
\& </html>
.Ve
.PP
foo.html:
.PP
.Vb 7
\& [% META
\& title = \*(AqThe Foo Page\*(Aq
\& author = \*(AqFred Foo\*(Aq
\& copyright = \*(Aq2000 Fred Foo\*(Aq
\& %]
\& <h1>[% template.title %]</h1>
\& Welcome to the Foo Page, blah blah blah
.Ve
.PP
output:
.PP
.Vb 10
\& <html>
\& <head>
\& <title>The Foo Page</title>
\& </head>
\& <body>
\& <!\-\- begin content \-\->
\& <h1>The Foo Page</h1>
\& Welcome to the Foo Page, blah blah blah
\& <!\-\- end content \-\->
\& <hr>
\& © Copyright 2000 Fred Foo
\& </body>
\& </html>
.Ve
.SS "\s-1WRAPPER\s0"
.IX Subsection "WRAPPER"
The \f(CW\*(C`WRAPPER\*(C'\fR 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 \f(CW\*(C`PROCESS\*(C'\fR template(s)) and
the output generated is then passed as the \f(CW\*(C`content\*(C'\fR variable to the
\&\f(CW\*(C`WRAPPER\*(C'\fR template(s) as they are processed.
.PP
.Vb 3
\& my $template = Template\->new({
\& WRAPPER => \*(Aqwrapper\*(Aq,
\& };
\&
\& # process \*(Aqfoo\*(Aq then wrap in \*(Aqwrapper\*(Aq
\& $template\->process(\*(Aqfoo\*(Aq, { message => \*(AqHello World!\*(Aq });
.Ve
.PP
wrapper:
.PP
.Vb 3
\& <wrapper>
\& [% content %]
\& </wrapper>
.Ve
.PP
foo:
.PP
.Vb 2
\& This is the foo file!
\& Message: [% message %]
.Ve
.PP
The output generated from this example is:
.PP
.Vb 4
\& <wrapper>
\& This is the foo file!
\& Message: Hello World!
\& </wrapper>
.Ve
.PP
You can specify more than one \f(CW\*(C`WRAPPER\*(C'\fR template by setting the value to
be a reference to a list of templates. The \f(CW\*(C`WRAPPER\*(C'\fR 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
\&\*(L"Does The Right Thing\*(R" to make wrapper templates nest in the order you
specify.
.PP
.Vb 3
\& my $template = Template\->new({
\& WRAPPER => [ \*(Aqouter\*(Aq, \*(Aqinner\*(Aq ],
\& };
\&
\& # process \*(Aqfoo\*(Aq then wrap in \*(Aqinner\*(Aq, then in \*(Aqouter\*(Aq
\& $template\->process(\*(Aqfoo\*(Aq, { message => \*(AqHello World!\*(Aq });
.Ve
.PP
outer:
.PP
.Vb 3
\& <outer>
\& [% content %]
\& </outer>
.Ve
.PP
inner:
.PP
.Vb 3
\& <inner>
\& [% content %]
\& </inner>
.Ve
.PP
The output generated is then:
.PP
.Vb 6
\& <outer>
\& <inner>
\& This is the foo file!
\& Message: Hello World!
\& </inner>
\& </outer>
.Ve
.PP
One side-effect of the \*(L"inside-out\*(R" processing of the \f(CW\*(C`WRAPPER\*(C'\fR
configuration item (and also the \f(CW\*(C`WRAPPER\*(C'\fR 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.
.PP
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:
.PP
foo:
.PP
.Vb 6
\& [% page = {
\& title = \*(AqFoo Page\*(Aq
\& subtitle = \*(AqEverything There is to Know About Foo\*(Aq
\& author = \*(AqFrank Oliver Octagon\*(Aq
\& }
\& %]
\&
\& <p>
\& Welcome to the page that tells you everything about foo
\& blah blah blah...
\& </p>
.Ve
.PP
The \f(CW\*(C`foo\*(C'\fR template is processed before the wrapper template meaning
that the \f(CW\*(C`page\*(C'\fR data structure will be defined for use in the wrapper
template.
.PP
wrapper:
.PP
.Vb 11
\& <html>
\& <head>
\& <title>[% page.title %]</title>
\& </head>
\& <body>
\& <h1>[% page.title %]</h1>
\& <h2>[% page.subtitle %]</h1>
\& <h3>by [% page.author %]</h3>
\& [% content %]
\& </body>
\& </html>
.Ve
.PP
It achieves the same effect as defining \f(CW\*(C`META\*(C'\fR items which are then
accessed via the \f(CW\*(C`template\*(C'\fR variable (which you are still free to
use within \f(CW\*(C`WRAPPER\*(C'\fR templates), but gives you more flexibility in
the type and complexity of data that you can define.
.SS "\s-1ERROR\s0"
.IX Subsection "ERROR"
The \f(CW\*(C`ERROR\*(C'\fR (or \f(CW\*(C`ERRORS\*(C'\fR 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.
.PP
If specified as a single value then that template will be processed
for all uncaught exceptions.
.PP
.Vb 3
\& my $template = Template\->new({
\& ERROR => \*(Aqerror.html\*(Aq
\& });
.Ve
.PP
If the \f(CW\*(C`ERROR\*(C'\fR 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 \f(CW\*(C`default\*(C'\fR template may be provided for the general
case. Note that \f(CW\*(C`ERROR\*(C'\fR can be pluralised to \f(CW\*(C`ERRORS\*(C'\fR if you find
it more appropriate in this case.
.PP
.Vb 7
\& my $template = Template\->new({
\& ERRORS => {
\& user => \*(Aquser/index.html\*(Aq,
\& dbi => \*(Aqerror/database\*(Aq,
\& default => \*(Aqerror/default\*(Aq,
\& },
\& });
.Ve
.PP
In this example, any \f(CW\*(C`user\*(C'\fR exceptions thrown will cause the
\&\fIuser/index.html\fR template to be processed, \f(CW\*(C`dbi\*(C'\fR errors are handled
by \fIerror/database\fR and all others by the \fIerror/default\fR template.
Any \f(CW\*(C`PRE_PROCESS\*(C'\fR and/or \f(CW\*(C`POST_PROCESS\*(C'\fR templates will also be applied
to these error templates.
.PP
Note that exception types are hierarchical and a \f(CW\*(C`foo\*(C'\fR handler will
catch all \f(CW\*(C`foo.*\*(C'\fR errors (e.g. \f(CW\*(C`foo.bar\*(C'\fR, \f(CW\*(C`foo.bar.baz\*(C'\fR) 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. \f(CW\*(C`user.passwd\*(C'\fR is parsed as \f(CW\*(Aquser\*(Aq.\*(Aqpasswd\*(Aq\fR).
.PP
.Vb 8
\& my $template = Template\->new({
\& ERROR => {
\& \*(Aquser.login\*(Aq => \*(Aquser/login.html\*(Aq,
\& \*(Aquser.passwd\*(Aq => \*(Aquser/badpasswd.html\*(Aq,
\& \*(Aquser\*(Aq => \*(Aquser/index.html\*(Aq,
\& \*(Aqdefault\*(Aq => \*(Aqerror/default\*(Aq,
\& },
\& });
.Ve
.PP
In this example, any template processed by the \f(CW$template\fR object, or
other templates or code called from within, can raise a \f(CW\*(C`user.login\*(C'\fR
exception and have the service redirect to the \fIuser/login.html\fR
template. Similarly, a \f(CW\*(C`user.passwd\*(C'\fR exception has a specific
handling template, \fIuser/badpasswd.html\fR, while all other \f(CW\*(C`user\*(C'\fR or
\&\f(CW\*(C`user.*\*(C'\fR exceptions cause a redirection to the \fIuser/index.html\fR page.
All other exception types are handled by \fIerror/default\fR.
.PP
Exceptions can be raised in a template using the \f(CW\*(C`THROW\*(C'\fR directive,
.PP
.Vb 1
\& [% THROW user.login \*(Aqno user id: please login\*(Aq %]
.Ve
.PP
or by calling the \fBthrow()\fR method on the
current Template::Context object,
.PP
.Vb 2
\& $context\->throw(\*(Aquser.passwd\*(Aq, \*(AqIncorrect Password\*(Aq);
\& $context\->throw(\*(AqIncorrect Password\*(Aq); # type \*(Aqundef\*(Aq
.Ve
.PP
or from Perl code by calling \f(CW\*(C`die()\*(C'\fR with a Template::Exception object,
.PP
.Vb 1
\& die (Template::Exception\->new(\*(Aquser.denied\*(Aq, \*(AqInvalid User ID\*(Aq));
.Ve
.PP
or by simply calling \fBdie()\fR with an error string. This is
automagically caught and converted to an exception of '\f(CW\*(C`undef\*(C'\fR'
type which can then be handled in the usual way.
.PP
.Vb 1
\& die "I\*(Aqm sorry Dave, I can\*(Aqt do that";
.Ve
.PP
Note that the '\f(CW\*(C`undef\*(C'\fR' we're talking about here is a literal string
rather than Perl's \f(CW\*(C`undef\*(C'\fR used to represent undefined values.
.SH "Template Runtime Options"
.IX Header "Template Runtime Options"
.SS "\s-1EVAL_PERL\s0"
.IX Subsection "EVAL_PERL"
This flag is used to indicate if \f(CW\*(C`PERL\*(C'\fR and/or \f(CW\*(C`RAWPERL\*(C'\fR blocks should be
evaluated. It is disabled by default and any \f(CW\*(C`PERL\*(C'\fR or \f(CW\*(C`RAWPERL\*(C'\fR blocks
encountered will raise exceptions of type '\f(CW\*(C`perl\*(C'\fR' with the message
\&'\f(CW\*(C`EVAL_PERL not set\*(C'\fR'. Note however that any \f(CW\*(C`RAWPERL\*(C'\fR blocks should
always contain valid Perl code, regardless of the \f(CW\*(C`EVAL_PERL\*(C'\fR flag. The
parser will fail to compile templates that contain invalid Perl code
in \f(CW\*(C`RAWPERL\*(C'\fR blocks and will throw a '\f(CW\*(C`file\*(C'\fR' exception.
.PP
When using compiled templates (see
\&\*(L"Caching and Compiling Options\*(R"),
the \f(CW\*(C`EVAL_PERL\*(C'\fR 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.
.PP
If the \f(CW\*(C`EVAL_PERL\*(C'\fR is set when a template is compiled, then all \f(CW\*(C`PERL\*(C'\fR and
\&\f(CW\*(C`RAWPERL\*(C'\fR blocks will be included in the compiled template. If the
\&\f(CW\*(C`EVAL_PERL\*(C'\fR option isn't set, then Perl code will be generated which
\&\fBalways\fR throws a '\f(CW\*(C`perl\*(C'\fR' exception with the message '\f(CW\*(C`EVAL_PERL not
set\*(C'\fR' \fBwhenever\fR the compiled template code is run.
.PP
Thus, you must have \f(CW\*(C`EVAL_PERL\*(C'\fR set if you want your compiled templates
to include \f(CW\*(C`PERL\*(C'\fR and \f(CW\*(C`RAWPERL\*(C'\fR blocks.
.PP
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 \f(CW\*(C`EVAL_PERL\*(C'\fR option was set at the time the
template was compiled, then the output of any \f(CW\*(C`RAWPERL\*(C'\fR blocks will be
included in the compiled template and will get executed when the
template is processed. This will happen regardless of the runtime
\&\f(CW\*(C`EVAL_PERL\*(C'\fR status.
.PP
Regular \f(CW\*(C`PERL\*(C'\fR blocks are a little more cautious, however. If the
\&\f(CW\*(C`EVAL_PERL\*(C'\fR flag isn't set for the \fIcurrent\fR context, that is, the
one which is trying to process it, then it will throw the familiar '\f(CW\*(C`perl\*(C'\fR'
exception with the message, '\f(CW\*(C`EVAL_PERL not set\*(C'\fR'.
.PP
Thus you can compile templates to include \f(CW\*(C`PERL\*(C'\fR blocks, but optionally
disable them when you process them later. Note however that it is
possible for a \f(CW\*(C`PERL\*(C'\fR block to contain a Perl "\f(CW\*(C`BEGIN { # some code }\*(C'\fR"
block which will always get run regardless of the runtime \f(CW\*(C`EVAL_PERL\*(C'\fR
status. Thus, if you set \f(CW\*(C`EVAL_PERL\*(C'\fR 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 \f(CW\*(C`EVAL_PERL\*(C'\fR disabled.
.SS "\s-1OUTPUT\s0"
.IX Subsection "OUTPUT"
Default output location or handler. This may be specified as one of:
a file name (relative to \f(CW\*(C`OUTPUT_PATH\*(C'\fR, if defined, or the current
working directory if not specified absolutely); a file handle
(e.g. \f(CW\*(C`GLOB\*(C'\fR or IO::Handle) 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
\&\f(CW\*(C`push()\*(C'\fRed; or as a reference to any object that supports the \f(CW\*(C`print()\*(C'\fR
method. This latter option includes the \f(CW\*(C`Apache::Request\*(C'\fR object which
is passed as the argument to Apache/mod_perl handlers.
.PP
example 1 (file name):
.PP
.Vb 3
\& my $template = Template\->new({
\& OUTPUT => "/tmp/foo",
\& });
.Ve
.PP
example 2 (text string):
.PP
.Vb 4
\& my $output = \*(Aq\*(Aq;
\& my $template = Template\->new({
\& OUTPUT => \e$output,
\& });
.Ve
.PP
example 3 (file handle):
.PP
.Vb 4
\& open (TOUT, ">", $file) || die "$file: $!\en";
\& my $template = Template\->new({
\& OUTPUT => \e*TOUT,
\& });
.Ve
.PP
example 4 (subroutine):
.PP
.Vb 4
\& sub output { my $out = shift; print "OUTPUT: $out" }
\& my $template = Template\->new({
\& OUTPUT => \e&output,
\& });
.Ve
.PP
example 5 (array reference):
.PP
.Vb 3
\& my $template = Template\->new({
\& OUTPUT => \e@output,
\& })
.Ve
.PP
example 6 (Apache/mod_perl handler):
.PP
.Vb 7
\& sub handler {
\& my $r = shift;
\& my $t = Template\->new({
\& OUTPUT => $r,
\& });
\& ...
\& }
.Ve
.PP
The default \f(CW\*(C`OUTPUT\*(C'\fR location be overridden by passing a third parameter to
the Template \fBprocess()\fR method. This can be specified
as any of the above argument types.
.PP
.Vb 6
\& $t\->process($file, $vars, "/tmp/foo");
\& $t\->process($file, $vars, \e$output);
\& $t\->process($file, $vars, \e*MYGLOB);
\& $t\->process($file, $vars, \e@output);
\& $t\->process($file, $vars, $r); # Apache::Request
\& ...
.Ve
.SS "\s-1OUTPUT_PATH\s0"
.IX Subsection "OUTPUT_PATH"
The \f(CW\*(C`OUTPUT_PATH\*(C'\fR allows a directory to be specified into which output
files should be written. An output file can be specified by the
\&\f(CW\*(C`OUTPUT\*(C'\fR option, or passed by name as the third parameter to the
Template \fBprocess()\fR method.
.PP
.Vb 4
\& my $template = Template\->new({
\& INCLUDE_PATH => "/tmp/src",
\& OUTPUT_PATH => "/tmp/dest",
\& });
\&
\& my $vars = {
\& ...
\& };
\&
\& foreach my $file (\*(Aqfoo.html\*(Aq, \*(Aqbar.html\*(Aq) {
\& $template\->process($file, $vars, $file)
\& || die $template\->error();
\& }
.Ve
.PP
This example will read the input files \fI/tmp/src/foo.html\fR and
\&\fI/tmp/src/bar.html\fR and write the processed output to \fI/tmp/dest/foo.html\fR
and \fI/tmp/dest/bar.html\fR, respectively.
.SS "\s-1STRICT\s0"
.IX Subsection "STRICT"
By default the Template Toolkit will silently ignore the use of undefined
variables (a bad design decision that I regret).
.PP
When the \f(CW\*(C`STRICT\*(C'\fR option is set, the use of any undefined variables or
values will cause an exception to be throw. The exception will have a
\&\f(CW\*(C`type\*(C'\fR of \f(CW\*(C`var.undef\*(C'\fR and a message of the form
\&\*(L"undefined variable: xxx\*(R".
.PP
.Vb 3
\& my $template = Template\->new(
\& STRICT => 1
\& );
.Ve
.SS "\s-1DEBUG\s0"
.IX Subsection "DEBUG"
The \f(CW\*(C`DEBUG\*(C'\fR option can be used to enable debugging within the various
different modules that comprise the Template Toolkit. The
Template::Constants module defines a set of
\&\f(CW\*(C`DEBUG_XXXX\*(C'\fR constants which can be combined using the logical \s-1OR\s0
operator, '\f(CW\*(C`|\*(C'\fR'.
.PP
.Vb 1
\& use Template::Constants qw( :debug );
\&
\& my $template = Template\->new({
\& DEBUG => DEBUG_PARSER | DEBUG_PROVIDER,
\& });
.Ve
.PP
For convenience, you can also provide a string containing a list
of lower case debug options, separated by any non-word characters.
.PP
.Vb 3
\& my $template = Template\->new({
\& DEBUG => \*(Aqparser, provider\*(Aq,
\& });
.Ve
.PP
The following \f(CW\*(C`DEBUG_XXXX\*(C'\fR flags can be used:
.IP "\s-1DEBUG_SERVICE\s0" 4
.IX Item "DEBUG_SERVICE"
Enables general debugging messages for the
Template::Service module.
.IP "\s-1DEBUG_CONTEXT\s0" 4
.IX Item "DEBUG_CONTEXT"
Enables general debugging messages for the
Template::Context module.
.IP "\s-1DEBUG_PROVIDER\s0" 4
.IX Item "DEBUG_PROVIDER"
Enables general debugging messages for the
Template::Provider module.
.IP "\s-1DEBUG_PLUGINS\s0" 4
.IX Item "DEBUG_PLUGINS"
Enables general debugging messages for the
Template::Plugins module.
.IP "\s-1DEBUG_FILTERS\s0" 4
.IX Item "DEBUG_FILTERS"
Enables general debugging messages for the
Template::Filters module.
.IP "\s-1DEBUG_PARSER\s0" 4
.IX Item "DEBUG_PARSER"
This flag causes the Template::Parser to generate
debugging messages that show the Perl code generated by parsing and
compiling each template.
.IP "\s-1DEBUG_UNDEF\s0" 4
.IX Item "DEBUG_UNDEF"
This option causes the Template Toolkit to throw an '\f(CW\*(C`undef\*(C'\fR' error
whenever it encounters an undefined variable value.
.IP "\s-1DEBUG_DIRS\s0" 4
.IX Item "DEBUG_DIRS"
This option causes the Template Toolkit to generate comments
indicating the source file, line and original text of each directive
in the template. These comments are embedded in the template output
using the format defined in the \f(CW\*(C`DEBUG_FORMAT\*(C'\fR configuration item, or a
simple default format if unspecified.
.Sp
For example, the following template fragment:
.Sp
.Vb 1
\& Hello World
.Ve
.Sp
would generate this output:
.Sp
.Vb 4
\& ## input text line 1 : ##
\& Hello
\& ## input text line 2 : World ##
\& World
.Ve
.IP "\s-1DEBUG_ALL\s0" 4
.IX Item "DEBUG_ALL"
Enables all debugging messages.
.IP "\s-1DEBUG_CALLER\s0" 4
.IX Item "DEBUG_CALLER"
This option causes all debug messages that aren't newline terminated
to have the file name and line number of the caller appended to them.
.SS "\s-1DEBUG_FORMAT\s0"
.IX Subsection "DEBUG_FORMAT"
The \f(CW\*(C`DEBUG_FORMAT\*(C'\fR option can be used to specify a format string for the
debugging messages generated via the \f(CW\*(C`DEBUG_DIRS\*(C'\fR option described
above. Any occurrences of \f(CW$file\fR, \f(CW$line\fR or \f(CW$text\fR will be
replaced with the current file name, line or directive text,
respectively. Notice how the format is single quoted to prevent Perl
from interpolating those tokens as variables.
.PP
.Vb 4
\& my $template = Template\->new({
\& DEBUG => \*(Aqdirs\*(Aq,
\& DEBUG_FORMAT => \*(Aq<!\-\- $file line $line : [% $text %] \-\->\*(Aq,
\& });
.Ve
.PP
The following template fragment:
.PP
.Vb 2
\& [% foo = \*(AqWorld\*(Aq %]
\& Hello [% foo %]
.Ve
.PP
would then generate this output:
.PP
.Vb 2
\& <!\-\- input text line 2 : [% foo = \*(AqWorld\*(Aq %] \-\->
\& Hello <!\-\- input text line 3 : [% foo %] \-\->World
.Ve
.PP
The \s-1DEBUG\s0 directive can also be used to set a debug format within
a template.
.PP
.Vb 1
\& [% DEBUG format \*(Aq<!\-\- $file line $line : [% $text %] \-\->\*(Aq %]
.Ve
.SH "Caching and Compiling Options"
.IX Header "Caching and Compiling Options"
.SS "\s-1CACHE_SIZE\s0"
.IX Subsection "CACHE_SIZE"
The Template::Provider module caches compiled templates to avoid the need
to re-parse template files or blocks each time they are used. The \f(CW\*(C`CACHE_SIZE\*(C'\fR
option is used to limit the number of compiled templates that the module
should cache.
.PP
By default, the \f(CW\*(C`CACHE_SIZE\*(C'\fR is undefined and all compiled templates are
cached. When set to any positive value, the cache will be limited to
storing no more than that number of compiled templates. When a new
template is loaded and compiled and the cache is full (i.e. the number
of entries == \f(CW\*(C`CACHE_SIZE\*(C'\fR), the least recently used compiled template
is discarded to make room for the new one.
.PP
The \f(CW\*(C`CACHE_SIZE\*(C'\fR can be set to \f(CW0\fR to disable caching altogether.
.PP
.Vb 3
\& my $template = Template\->new({
\& CACHE_SIZE => 64, # only cache 64 compiled templates
\& });
\&
\& my $template = Template\->new({
\& CACHE_SIZE => 0, # don\*(Aqt cache any compiled templates
\& });
.Ve
.PP
As well as caching templates as they are found, the Template::Provider
also implements negative caching to keep track of templates that are
\&\fInot\fR found. This allows the provider to quickly decline a request
for a template that it has previously failed to locate, saving the effort
of going to look for it again. This is useful when an \f(CW\*(C`INCLUDE_PATH\*(C'\fR includes
multiple providers, ensuring that the request is passed down through the
providers as quickly as possible.
.SS "\s-1STAT_TTL\s0"
.IX Subsection "STAT_TTL"
This value can be set to control how long the Template::Provider will keep a
template cached in memory before checking to see if the source template has
changed.
.PP
.Vb 3
\& my $provider = Template::Provider\->new({
\& STAT_TTL => 60, # one minute
\& });
.Ve
.PP
The default value is 1 (second). You'll probably want to set this to a higher
value if you're running the Template Toolkit inside a persistent web server
application (e.g. mod_perl). For example, set it to 60 and the provider will
only look for changes to templates once a minute at most. However, during
development (or any time you're making frequent changes to templates) you'll
probably want to keep it set to a low value so that you don't have to wait
for the provider to notice that your templates have changed.
.SS "\s-1COMPILE_EXT\s0"
.IX Subsection "COMPILE_EXT"
From version 2 onwards, the Template Toolkit has the ability to
compile templates to Perl code and save them to disk for subsequent
use (i.e. cache persistence). The \f(CW\*(C`COMPILE_EXT\*(C'\fR option may be
provided to specify a filename extension for compiled template files.
It is undefined by default and no attempt will be made to read or write
any compiled template files.
.PP
.Vb 3
\& my $template = Template\->new({
\& COMPILE_EXT => \*(Aq.ttc\*(Aq,
\& });
.Ve
.PP
If \f(CW\*(C`COMPILE_EXT\*(C'\fR is defined (and \f(CW\*(C`COMPILE_DIR\*(C'\fR isn't, see below) then compiled
template files with the \f(CW\*(C`COMPILE_EXT\*(C'\fR extension will be written to the same
directory from which the source template files were loaded.
.PP
Compiling and subsequent reuse of templates happens automatically
whenever the \f(CW\*(C`COMPILE_EXT\*(C'\fR or \f(CW\*(C`COMPILE_DIR\*(C'\fR options are set. The Template
Toolkit will automatically reload and reuse compiled files when it
finds them on disk. If the corresponding source file has been modified
since the compiled version as written, then it will load and re-compile
the source and write a new compiled version to disk.
.PP
This form of cache persistence offers significant benefits in terms of
time and resources required to reload templates. Compiled templates can
be reloaded by a simple call to Perl's \f(CW\*(C`require()\*(C'\fR, leaving Perl to handle
all the parsing and compilation. This is a Good Thing.
.SS "\s-1COMPILE_DIR\s0"
.IX Subsection "COMPILE_DIR"
The \f(CW\*(C`COMPILE_DIR\*(C'\fR option is used to specify an alternate directory root
under which compiled template files should be saved.
.PP
.Vb 3
\& my $template = Template\->new({
\& COMPILE_DIR => \*(Aq/tmp/ttc\*(Aq,
\& });
.Ve
.PP
The \f(CW\*(C`COMPILE_EXT\*(C'\fR option may also be specified to have a consistent file
extension added to these files.
.PP
.Vb 4
\& my $template1 = Template\->new({
\& COMPILE_DIR => \*(Aq/tmp/ttc\*(Aq,
\& COMPILE_EXT => \*(Aq.ttc1\*(Aq,
\& });
\&
\& my $template2 = Template\->new({
\& COMPILE_DIR => \*(Aq/tmp/ttc\*(Aq,
\& COMPILE_EXT => \*(Aq.ttc2\*(Aq,
\& });
.Ve
.PP
When \f(CW\*(C`COMPILE_EXT\*(C'\fR is undefined, the compiled template files have the
same name as the original template files, but reside in a different
directory tree.
.PP
Each directory in the \f(CW\*(C`INCLUDE_PATH\*(C'\fR is replicated in full beneath the
\&\f(CW\*(C`COMPILE_DIR\*(C'\fR directory. This example:
.PP
.Vb 4
\& my $template = Template\->new({
\& COMPILE_DIR => \*(Aq/tmp/ttc\*(Aq,
\& INCLUDE_PATH => \*(Aq/home/abw/templates:/usr/share/templates\*(Aq,
\& });
.Ve
.PP
would create the following directory structure:
.PP
.Vb 2
\& /tmp/ttc/home/abw/templates/
\& /tmp/ttc/usr/share/templates/
.Ve
.PP
Files loaded from different \f(CW\*(C`INCLUDE_PATH\*(C'\fR directories will have their
compiled forms save in the relevant \f(CW\*(C`COMPILE_DIR\*(C'\fR directory.
.PP
On Win32 platforms a filename may by prefixed by a drive letter and
colon. e.g.
.PP
.Vb 1
\& C:/My Templates/header
.Ve
.PP
The colon will be silently stripped from the filename when it is added
to the \f(CW\*(C`COMPILE_DIR\*(C'\fR value(s) to prevent illegal filename being generated.
Any colon in \f(CW\*(C`COMPILE_DIR\*(C'\fR elements will be left intact. For example:
.PP
.Vb 6
\& # Win32 only
\& my $template = Template\->new({
\& DELIMITER => \*(Aq;\*(Aq,
\& COMPILE_DIR => \*(AqC:/TT2/Cache\*(Aq,
\& INCLUDE_PATH => \*(AqC:/TT2/Templates;D:/My Templates\*(Aq,
\& });
.Ve
.PP
This would create the following cache directories:
.PP
.Vb 2
\& C:/TT2/Cache/C/TT2/Templates
\& C:/TT2/Cache/D/My Templates
.Ve
.SH "Plugins and Filters"
.IX Header "Plugins and Filters"
.SS "\s-1PLUGINS\s0"
.IX Subsection "PLUGINS"
The \f(CW\*(C`PLUGINS\*(C'\fR options can be used to provide a reference to a hash array
that maps plugin names to Perl module names. A number of standard
plugins are defined (e.g. \f(CW\*(C`table\*(C'\fR, \f(CW\*(C`format\*(C'\fR, \f(CW\*(C`cgi\*(C'\fR, etc.) which map to
their corresponding \f(CW\*(C`Template::Plugin::*\*(C'\fR counterparts. These can be
redefined by values in the \f(CW\*(C`PLUGINS\*(C'\fR hash.
.PP
.Vb 7
\& my $template = Template\->new({
\& PLUGINS => {
\& cgi => \*(AqMyOrg::Template::Plugin::CGI\*(Aq,
\& foo => \*(AqMyOrg::Template::Plugin::Foo\*(Aq,
\& bar => \*(AqMyOrg::Template::Plugin::Bar\*(Aq,
\& },
\& });
.Ve
.PP
The recommended convention is to specify these plugin names in lower
case. The Template Toolkit first looks for an exact case-sensitive
match and then tries the lower case conversion of the name specified.
.PP
.Vb 1
\& [% USE Foo %] # look for \*(AqFoo\*(Aq then \*(Aqfoo\*(Aq
.Ve
.PP
If you define all your \f(CW\*(C`PLUGINS\*(C'\fR with lower case names then they will be
located regardless of how the user specifies the name in the \s-1USE\s0
directive. If, on the other hand, you define your \f(CW\*(C`PLUGINS\*(C'\fR with upper
or mixed case names then the name specified in the \f(CW\*(C`USE\*(C'\fR directive must
match the case exactly.
.PP
The \f(CW\*(C`USE\*(C'\fR directive is used to create plugin objects and does so by calling
the \fBplugin()\fR method on the current
Template::Context object. If the plugin name is defined in the \f(CW\*(C`PLUGINS\*(C'\fR
hash then the corresponding Perl module is loaded via \f(CW\*(C`require()\*(C'\fR. The
context then calls the \fBload()\fR class method which
should return the class name (default and general case) or a prototype object
against which the \fBnew()\fR method can be called to
instantiate individual plugin objects.
.PP
If the plugin name is not defined in the \f(CW\*(C`PLUGINS\*(C'\fR hash then the
\&\f(CW\*(C`PLUGIN_BASE\*(C'\fR and/or \f(CW\*(C`LOAD_PERL\*(C'\fR options come into effect.
.SS "\s-1PLUGIN_BASE\s0"
.IX Subsection "PLUGIN_BASE"
If a plugin is not defined in the \f(CW\*(C`PLUGINS\*(C'\fR hash then the \f(CW\*(C`PLUGIN_BASE\*(C'\fR is used
to attempt to construct a correct Perl module name which can be successfully
loaded.
.PP
The \f(CW\*(C`PLUGIN_BASE\*(C'\fR can be specified as a reference to an array of module
namespaces, or as a single value which is automatically converted to a
list. The default \f(CW\*(C`PLUGIN_BASE\*(C'\fR value (\f(CW\*(C`Template::Plugin\*(C'\fR) is then added
to the end of this list.
.PP
example 1:
.PP
.Vb 3
\& my $template = Template\->new({
\& PLUGIN_BASE => \*(AqMyOrg::Template::Plugin\*(Aq,
\& });
\&
\& [% USE Foo %] # => MyOrg::Template::Plugin::Foo
\& or Template::Plugin::Foo
.Ve
.PP
example 2:
.PP
.Vb 4
\& my $template = Template\->new({
\& PLUGIN_BASE => [ \*(AqMyOrg::Template::Plugin\*(Aq,
\& \*(AqYourOrg::Template::Plugin\*(Aq ],
\& });
.Ve
.PP
template:
.PP
.Vb 3
\& [% USE Foo %] # => MyOrg::Template::Plugin::Foo
\& or YourOrg::Template::Plugin::Foo
\& or Template::Plugin::Foo
.Ve
.PP
If you don't want the default \f(CW\*(C`Template::Plugin\*(C'\fR namespace added to the
end of the \f(CW\*(C`PLUGIN_BASE\*(C'\fR, then set the \f(CW$Template::Plugins::PLUGIN_BASE\fR
variable to a false value before calling the \fBnew()\fR Template#\fBnew()\fR
constructor method. This is shown in the example below where the
\&\f(CW\*(C`Foo\*(C'\fR plugin is located as \f(CW\*(C`My::Plugin::Foo\*(C'\fR or \f(CW\*(C`Your::Plugin::Foo\*(C'\fR but not
as \f(CW\*(C`Template::Plugin::Foo\*(C'\fR.
.PP
example 3:
.PP
.Vb 2
\& use Template::Plugins;
\& $Template::Plugins::PLUGIN_BASE = \*(Aq\*(Aq;
\&
\& my $template = Template\->new({
\& PLUGIN_BASE => [ \*(AqMy::Plugin\*(Aq,
\& \*(AqYour::Plugin\*(Aq ],
\& });
.Ve
.PP
template:
.PP
.Vb 2
\& [% USE Foo %] # => My::Plugin::Foo
\& or Your::Plugin::Foo
.Ve
.SS "\s-1LOAD_PERL\s0"
.IX Subsection "LOAD_PERL"
If a plugin cannot be loaded using the \f(CW\*(C`PLUGINS\*(C'\fR or \f(CW\*(C`PLUGIN_BASE\*(C'\fR
approaches then the provider can make a final attempt to load the
module without prepending any prefix to the module path. This allows
regular Perl modules (i.e. those that don't reside in the
Template::Plugin or some other such namespace) to be loaded and used
as plugins.
.PP
By default, the \f(CW\*(C`LOAD_PERL\*(C'\fR option is set to \f(CW0\fR and no attempt will be made
to load any Perl modules that aren't named explicitly in the \f(CW\*(C`PLUGINS\*(C'\fR
hash or reside in a package as named by one of the \f(CW\*(C`PLUGIN_BASE\*(C'\fR
components.
.PP
Plugins loaded using the \f(CW\*(C`PLUGINS\*(C'\fR or \f(CW\*(C`PLUGIN_BASE\*(C'\fR receive a reference to
the current context object as the first argument to the
\&\fBnew()\fR constructor. Modules loaded using \f(CW\*(C`LOAD_PERL\*(C'\fR
are assumed to not conform to the plugin interface. They must provide a \f(CW\*(C`new()\*(C'\fR
class method for instantiating objects but it will not receive a reference to
the context as the first argument.
.PP
Plugin modules should provide a \fBload()\fR class method
(or inherit the default one from the Template::Plugin base class) which is
called the first time the plugin is loaded. Regular Perl modules need not. In
all other respects, regular Perl objects and Template Toolkit plugins are
identical.
.PP
If a particular Perl module does not conform to the common, but not
unilateral, \f(CW\*(C`new()\*(C'\fR constructor convention then a simple plugin wrapper
can be written to interface to it.
.SS "\s-1FILTERS\s0"
.IX Subsection "FILTERS"
The \f(CW\*(C`FILTERS\*(C'\fR option can be used to specify custom filters which can
then be used with the \f(CW\*(C`FILTER\*(C'\fR directive like any other. These are
added to the standard filters which are available by default. Filters
specified via this option will mask any standard filters of the same
name.
.PP
The \f(CW\*(C`FILTERS\*(C'\fR option should be specified as a reference to a hash array
in which each key represents the name of a filter. The corresponding
value should contain a reference to an array containing a subroutine
reference and a flag which indicates if the filter is static (\f(CW0\fR) or
dynamic (\f(CW1\fR). A filter may also be specified as a solitary subroutine
reference and is assumed to be static.
.PP
.Vb 7
\& $template = Template\->new({
\& FILTERS => {
\& \*(Aqsfilt1\*(Aq => \e&static_filter, # static
\& \*(Aqsfilt2\*(Aq => [ \e&static_filter, 0 ], # same as above
\& \*(Aqdfilt1\*(Aq => [ \e&dyanamic_filter_factory, 1 ],
\& },
\& });
.Ve
.PP
Additional filters can be specified at any time by calling the
\&\fBdefine_filter()\fR method on the current
Template::Context object. The method accepts a filter name, a reference to a
filter subroutine and an optional flag to indicate if the filter is dynamic.
.PP
.Vb 3
\& my $context = $template\->context();
\& $context\->define_filter(\*(Aqnew_html\*(Aq, \e&new_html);
\& $context\->define_filter(\*(Aqnew_repeat\*(Aq, \e&new_repeat, 1);
.Ve
.PP
Static filters are those where a single subroutine reference is used
for all invocations of a particular filter. Filters that don't accept
any configuration parameters (e.g. \f(CW\*(C`html\*(C'\fR) can be implemented
statically. The subroutine reference is simply returned when that
particular filter is requested. The subroutine is called to filter
the output of a template block which is passed as the only argument.
The subroutine should return the modified text.
.PP
.Vb 5
\& sub static_filter {
\& my $text = shift;
\& # do something to modify $text...
\& return $text;
\& }
.Ve
.PP
The following template fragment:
.PP
.Vb 3
\& [% FILTER sfilt1 %]
\& Blah blah blah.
\& [% END %]
.Ve
.PP
is approximately equivalent to:
.PP
.Vb 1
\& &static_filter("\enBlah blah blah.\en");
.Ve
.PP
Filters that can accept parameters (e.g. \f(CW\*(C`truncate\*(C'\fR) should be
implemented dynamically. In this case, the subroutine is taken to be
a filter 'factory' that is called to create a unique filter subroutine
each time one is requested. A reference to the current
Template::Context object is passed as the first parameter, followed by
any additional parameters specified. The subroutine should return
another subroutine reference (usually a closure) which implements the
filter.
.PP
.Vb 2
\& sub dynamic_filter_factory {
\& my ($context, @args) = @_;
\&
\& return sub {
\& my $text = shift;
\& # do something to modify $text...
\& return $text;
\& }
\& }
.Ve
.PP
The following template fragment:
.PP
.Vb 3
\& [% FILTER dfilt1(123, 456) %]
\& Blah blah blah
\& [% END %]
.Ve
.PP
is approximately equivalent to:
.PP
.Vb 2
\& my $filter = &dynamic_filter_factory($context, 123, 456);
\& &$filter("\enBlah blah blah.\en");
.Ve
.PP
See the \f(CW\*(C`FILTER\*(C'\fR directive for further examples.
.SH "Customisation and Extension"
.IX Header "Customisation and Extension"
.SS "\s-1LOAD_TEMPLATES\s0"
.IX Subsection "LOAD_TEMPLATES"
The \f(CW\*(C`LOAD_TEMPLATES\*(C'\fR option can be used to provide a reference to a list
of Template::Provider objects or sub-classes thereof which will take
responsibility for loading and compiling templates.
.PP
.Vb 6
\& my $template = Template\->new({
\& LOAD_TEMPLATES => [
\& MyOrg::Template::Provider\->new({ ... }),
\& Template::Provider\->new({ ... }),
\& ],
\& });
.Ve
.PP
When a \f(CW\*(C`PROCESS\*(C'\fR, \f(CW\*(C`INCLUDE\*(C'\fR or \f(CW\*(C`WRAPPER\*(C'\fR directive is encountered, the
named template may refer to a locally defined \f(CW\*(C`BLOCK\*(C'\fR or a file relative to
the \f(CW\*(C`INCLUDE_PATH\*(C'\fR (or an absolute or relative path if the appropriate
\&\f(CW\*(C`ABSOLUTE\*(C'\fR or \f(CW\*(C`RELATIVE\*(C'\fR options are set). If a \f(CW\*(C`BLOCK\*(C'\fR definition can't be
found (see the Template::Context \fBtemplate()\fR
method for a discussion of \f(CW\*(C`BLOCK\*(C'\fR locality) then each of the
\&\f(CW\*(C`LOAD_TEMPLATES\*(C'\fR provider objects is queried in turn via the
\&\fBfetch()\fR method to see if it can supply the
required template.
.PP
Each provider can return a compiled template, an error, or decline to service
the request in which case the responsibility is passed to the next provider.
If none of the providers can service the request then a 'not found' error is
returned. The same basic provider mechanism is also used for the \f(CW\*(C`INSERT\*(C'\fR
directive but it bypasses any \f(CW\*(C`BLOCK\*(C'\fR definitions and doesn't attempt is to
parse or process the contents of the template file.
.PP
If \f(CW\*(C`LOAD_TEMPLATES\*(C'\fR is undefined, a single default provider will be
instantiated using the current configuration parameters. For example, the
Template::Provider \f(CW\*(C`INCLUDE_PATH\*(C'\fR option can be specified in the Template
configuration and will be correctly passed to the provider's constructor
method.
.PP
.Vb 3
\& my $template = Template\->new({
\& INCLUDE_PATH => \*(Aq/here:/there\*(Aq,
\& });
.Ve
.SS "\s-1LOAD_PLUGINS\s0"
.IX Subsection "LOAD_PLUGINS"
The \f(CW\*(C`LOAD_PLUGINS\*(C'\fR options can be used to specify a list of provider objects
(i.e. they implement the \fBfetch()\fR method) which
are responsible for loading and instantiating template plugin objects. The
Template::Context \fBplugin()\fR method queries
each provider in turn in a \*(L"Chain of Responsibility\*(R" as per the
\&\fBtemplate()\fR and
\&\fBfilter()\fR methods.
.PP
.Vb 6
\& my $template = Template\->new({
\& LOAD_PLUGINS => [
\& MyOrg::Template::Plugins\->new({ ... }),
\& Template::Plugins\->new({ ... }),
\& ],
\& });
.Ve
.PP
By default, a single Template::Plugins object is created using the
current configuration hash. Configuration items destined for the
Template::Plugins constructor may be added to the Template
constructor.
.PP
.Vb 4
\& my $template = Template\->new({
\& PLUGIN_BASE => \*(AqMyOrg::Template::Plugins\*(Aq,
\& LOAD_PERL => 1,
\& });
.Ve
.SS "\s-1LOAD_FILTERS\s0"
.IX Subsection "LOAD_FILTERS"
The \f(CW\*(C`LOAD_FILTERS\*(C'\fR option can be used to specify a list of provider objects
(i.e. they implement the \fBfetch()\fR method) which
are responsible for returning and/or creating filter subroutines. The
Template::Context \fBfilter()\fR method queries
each provider in turn in a \*(L"Chain of Responsibility\*(R" as per the
\&\fBtemplate()\fR and
\&\fBplugin()\fR methods.
.PP
.Vb 6
\& my $template = Template\->new({
\& LOAD_FILTERS => [
\& MyTemplate::Filters\->new(),
\& Template::Filters\->new(),
\& ],
\& });
.Ve
.PP
By default, a single Template::Filters object is created for the
\&\f(CW\*(C`LOAD_FILTERS\*(C'\fR list.
.SS "\s-1TOLERANT\s0"
.IX Subsection "TOLERANT"
The \f(CW\*(C`TOLERANT\*(C'\fR flag is used by the various Template Toolkit provider modules
(Template::Provider, Template::Plugins, Template::Filters) to control
their behaviour when errors are encountered. By default, any errors are
reported as such, with the request for the particular resource (\f(CW\*(C`template\*(C'\fR,
\&\f(CW\*(C`plugin\*(C'\fR, \f(CW\*(C`filter\*(C'\fR) being denied and an exception raised.
.PP
When the \f(CW\*(C`TOLERANT\*(C'\fR flag is set to any true values, errors will be silently
ignored and the provider will instead return \f(CW\*(C`STATUS_DECLINED\*(C'\fR. This allows a
subsequent provider to take responsibility for providing the resource, rather
than failing the request outright. If all providers decline to service the
request, either through tolerated failure or a genuine disinclination to
comply, then a '\f(CW\*(C`<resource> not found\*(C'\fR' exception is raised.
.SS "\s-1SERVICE\s0"
.IX Subsection "SERVICE"
A reference to a Template::Service object, or sub-class thereof, to which
the Template module should delegate. If unspecified, a Template::Service
object is automatically created using the current configuration hash.
.PP
.Vb 3
\& my $template = Template\->new({
\& SERVICE => MyOrg::Template::Service\->new({ ... }),
\& });
.Ve
.SS "\s-1CONTEXT\s0"
.IX Subsection "CONTEXT"
A reference to a Template::Context object which is used to define a
specific environment in which template are processed. A Template::Context
object is passed as the only parameter to the Perl subroutines that represent
\&\*(L"compiled\*(R" template documents. Template subroutines make callbacks into the
context object to access Template Toolkit functionality, for example, to
\&\f(CW\*(C`INCLUDE\*(C'\fR or \f(CW\*(C`PROCESS\*(C'\fR another template
(\fBinclude()\fR and
\&\fBprocess()\fR methods, respectively), to \f(CW\*(C`USE\*(C'\fR a
plugin (\fBplugin()\fR) or instantiate a filter
(\fBfilter()\fR) or to access the stash
(\fBstash()\fR) which manages variable definitions via
the \fBget()\fR and \fBset()\fR methods.
.PP
.Vb 3
\& my $template = Template\->new({
\& CONTEXT => MyOrg::Template::Context\->new({ ... }),
\& });
.Ve
.SS "\s-1STASH\s0"
.IX Subsection "STASH"
A reference to a Template::Stash object or sub-class which will take
responsibility for managing template variables.
.PP
.Vb 4
\& my $stash = MyOrg::Template::Stash\->new({ ... });
\& my $template = Template\->new({
\& STASH => $stash,
\& });
.Ve
.PP
If unspecified, a default stash object is created using the \f(CW\*(C`VARIABLES\*(C'\fR
configuration item to initialise the stash variables.
.PP
.Vb 6
\& my $template = Template\->new({
\& VARIABLES => {
\& id => \*(Aqabw\*(Aq,
\& name => \*(AqAndy Wardley\*(Aq,
\& },
\& };
.Ve
.SS "\s-1PARSER\s0"
.IX Subsection "PARSER"
The Template::Parser module implements a parser object for compiling
templates into Perl code which can then be executed. A default object
of this class is created automatically and then used by the
Template::Provider whenever a template is loaded and requires
compilation. The \f(CW\*(C`PARSER\*(C'\fR option can be used to provide a reference to
an alternate parser object.
.PP
.Vb 3
\& my $template = Template\->new({
\& PARSER => MyOrg::Template::Parser\->new({ ... }),
\& });
.Ve
.SS "\s-1GRAMMAR\s0"
.IX Subsection "GRAMMAR"
The \f(CW\*(C`GRAMMAR\*(C'\fR configuration item can be used to specify an alternate
grammar for the parser. This allows a modified or entirely new
template language to be constructed and used by the Template Toolkit.
.PP
Source templates are compiled to Perl code by the Template::Parser
using the Template::Grammar (by default) to define the language
structure and semantics. Compiled templates are thus inherently
\&\*(L"compatible\*(R" with each other and there is nothing to prevent any
number of different template languages being compiled and used within
the same Template Toolkit processing environment (other than the usual
time and memory constraints).
.PP
The Template::Grammar file is constructed from a \s-1YACC\s0 like grammar
(using \f(CW\*(C`Parse::YAPP\*(C'\fR) and a skeleton module template. These files are
provided, along with a small script to rebuild the grammar, in the
\&\fIparser\fR sub-directory of the distribution.
.PP
You don't have to know or worry about these unless you want to hack on the
template language or define your own variant. There is a \fI\s-1README\s0\fR file in the
same directory which provides some small guidance but it is assumed that you
know what you're doing if you venture herein. If you grok \s-1LALR\s0 parsers, then
you should find it comfortably familiar.
.PP
By default, an instance of the default Template::Grammar will be
created and used automatically if a \f(CW\*(C`GRAMMAR\*(C'\fR item isn't specified.
.PP
.Vb 1
\& use MyOrg::Template::Grammar;
\&
\& my $template = Template\->new({
\& GRAMMAR = MyOrg::Template::Grammar\->new();
\& });
.Ve
Zerion Mini Shell 1.0