.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" 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 turned on, 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 .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Template::FAQ 3" .TH Template::FAQ 3 "2019-01-04" "perl v5.16.3" "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::FAQ \- Frequently Asked Questions about the Template Toolkit .SH "Template Toolkit Language" .IX Header "Template Toolkit Language" .SS "Why doesn't [% a = b \s-1IF\s0 c %] work as expected?" .IX Subsection "Why doesn't [% a = b IF c %] work as expected?" There's a limitation in the \s-1TT2\s0 parser which means that the following code doesn't work as you might expect: .PP .Vb 1 \& [% a = b IF c %] .Ve .PP The parser interprets it as an attempt to set \f(CW\*(C`a\*(C'\fR to the result of \&\f(CW\*(C`b IF c\*(C'\fR, like this: .PP .Vb 1 \& [% a = (b IF c) %] .Ve .PP If you want to set \f(CW\*(C`a = b\*(C'\fR only if \f(CW\*(C`c\*(C'\fR is true, then do this instead: .PP .Vb 1 \& [% SET a = b IF c %] .Ve .PP The explicit \f(CW\*(C`SET\*(C'\fR keyword gives the parser the clue it needs to do the right thing. .PP \&\s-1NOTE:\s0 this will be fixed in \s-1TT3\s0 .SS "If I'm using \s-1TT\s0 to write out a \s-1TT\s0 template, is there a good way to escape [% and %]?" .IX Subsection "If I'm using TT to write out a TT template, is there a good way to escape [% and %]?" You can do something like this: .PP .Vb 3 \& [% stag = "[\e%" \& etag = "%\e]" \& %] .Ve .PP and then: .PP .Vb 1 \& [% stag; \*(Aqhello\*(Aq; etag %] .Ve .PP Or you can use the \f(CW\*(C`TAGS\*(C'\fR directive, like so: .PP .Vb 3 \& [% TAGS [\- \-] %] \& [\- INCLUDE foo \-] # is a directive \& [% INCLUDE foo %] # not a directive .Ve .SS "How do I iterate over a hash?" .IX Subsection "How do I iterate over a hash?" This is covered in the Template::Manual::VMethods section of the manual. A list of all the keys that are in the hash can be obtained with the \&\f(CW\*(C`keys\*(C'\fR virtual method. You can then iterate over that list and by looking up each key in turn get the value. .PP .Vb 3 \& [% FOREACH key = product.keys %] \& [% key %] => [% product.$key %] \& [% END %] .Ve .SH "Plugins" .IX Header "Plugins" .SS "How do I get the Table plugin to order data across rather than down?" .IX Subsection "How do I get the Table plugin to order data across rather than down?" Order the data into rows: .PP .Vb 3 \& Steve Karen Jeff \& Brooklyn Nantucket Fairfax \& NY MA VA \& \& [% USE table(data, rows=3) %] .Ve .PP Then ask for each column .PP .Vb 1 \& [% FOREACH column = table.cols %] .Ve .PP And then print each item in the column going across the output rows .PP .Vb 3 \& [% FOREACH item = column %] \&
The value is [% CGI.cookie(\*(Aqcookie_name\*(Aq) | html %]
.Ve
.SH "Extending the Template Toolkit"
.IX Header "Extending the Template Toolkit"
.SS "Can I serve templates from a database?"
.IX Subsection "Can I serve templates from a database?"
Short answer: yes, Chris Nandor has done this for Slash. You need to
subclass Template::Provider. See the mailing list archives for further
info.
.SS "Can I fetch templates via http?"
.IX Subsection "Can I fetch templates via http?"
To do the job properly, you should subclass Template::Provider to
\&\f(CW\*(C`Template::Provider::HTTP\*(C'\fR and use a \f(CW\*(C`PREFIX_MAP\*(C'\fR option to bind the \f(CW\*(C`http\*(C'\fR
template prefix to that particular provider (you may want to go digging around
in the \fIChanges\fR file around version 2.01 for more info on \f(CW\*(C`PREFIX_MAP\*(C'\fR \- it
may not be properly documented anywhere else...yet!). e.g.
.PP
.Vb 1
\& use Template::Provider::HTTP;
\&
\& my $file = Template::Provider( INCLUDE_PATH => [...] );
\& my $http = Template::Provider::HTTP\->new(...);
\& my $tt2 = Template\->new({
\& LOAD_TEMPLATES => [ $file, $http ],
\& PREFIX_MAP => {
\& file => \*(Aq0\*(Aq, # file:foo.html
\& http => \*(Aq1\*(Aq, # http:foo.html
\& default => \*(Aq0\*(Aq, # foo.html => file:foo.html
\& }
\& });
.Ve
.PP
Now a template specified as:
.PP
.Vb 1
\& [% INCLUDE foo %]
.Ve
.PP
will be served by the 'file' provider (the default). Otherwise you
can explicitly add a prefix:
.PP
.Vb 3
\& [% INCLUDE file:foo.html %]
\& [% INCLUDE http:foo.html %]
\& [% INCLUDE http://www.xyz.com/tt2/header.tt2 %]
.Ve
.PP
This same principal can be used to create a \s-1DBI\s0 template provider. e.g.
.PP
.Vb 1
\& [% INCLUDE dbi:foo.html %]
.Ve
.PP
Alas, we don't yet have a \s-1DBI\s0 provider as part of the Template Toolkit. There
has been some talk on the mailing list about efforts to develop \s-1DBI\s0 and/or
\&\s-1HTTP\s0 providers but as yet no-one has stepped forward to take up the
challenge...
.PP
In the mean time, Craig Barrat's post from the mailing list has some useful
pointers on how to achieve this using existing modules. See