1Template::FAQ(3)      User Contributed Perl Documentation     Template::FAQ(3)
2
3
4

NAME

6       Template::FAQ - Frequently Asked Questions about the Template Toolkit
7

Template Toolkit Language

9   Why doesn't [% a = b IF c %] work as expected?
10       There's a limitation in the TT2 parser which means that the following
11       code doesn't work as you might expect:
12
13           [% a = b IF c %]
14
15       The parser interprets it as an attempt to set "a" to the result of "b
16       IF c", like this:
17
18           [% a = (b IF c) %]
19
20       If you want to set "a = b" only if "c" is true, then do this instead:
21
22           [% SET a = b IF c %]
23
24       The explicit "SET" keyword gives the parser the clue it needs to do the
25       right thing.
26
27       NOTE: this will be fixed in TT3
28
29   If I'm using TT to write out a TT template, is there a good way to escape
30       [% and %]?
31       You can do something like this:
32
33           [% stag = "[\%"
34              etag = "%\]"
35           %]
36
37       and then:
38
39           [% stag; 'hello'; etag %]
40
41       Or you can use the "TAGS" directive, like so:
42
43           [% TAGS [- -] %]
44           [- INCLUDE foo -]   # is a directive
45           [% INCLUDE foo %]   # not a directive
46
47   How do I iterate over a hash?
48       This is covered in the Template::Manual::VMethods section of the
49       manual. A list of all the keys that are in the hash can be obtained
50       with the "keys" virtual method. You can then iterate over that list and
51       by looking up each key in turn get the value.
52
53           [% FOREACH key = product.keys %]
54              [% key %] => [% product.$key %]
55           [% END %]
56

Plugins

58   How do I get the Table plugin to order data across rather than down?
59       Order the data into rows:
60
61           Steve     Karen     Jeff
62           Brooklyn  Nantucket Fairfax
63           NY        MA        VA
64
65           [% USE table(data, rows=3) %]
66
67       Then ask for each column
68
69           [% FOREACH column = table.cols %]
70
71       And then print each item in the column going across the output rows
72
73           [% FOREACH item = column %]
74               <td>[% item %]</td>
75           [% END %]
76
77   Accessing Cookies
78       Jeff Boes <jboes@nexcerpt.com> asks:
79
80           Does anyone have a quick-n-dirty approach to accessing
81           cookies from templates?
82
83       Jonas Liljegren answers:
84
85           [% USE CGI %]
86
87           <p>The value is [% CGI.cookie('cookie_name') | html %]
88
89       You will need to have Template::Plugin::CGI installed.
90

Extending the Template Toolkit

92   Can I serve templates from a database?
93       Short answer: yes, Chris Nandor has done this for Slash.  You need to
94       subclass Template::Provider.  See the mailing list archives for further
95       info.
96
97   Can I fetch templates via http?
98       To do the job properly, you should subclass Template::Provider to
99       "Template::Provider::HTTP" and use a "PREFIX_MAP" option to bind the
100       "http" template prefix to that particular provider (you may want to go
101       digging around in the Changes file around version 2.01 for more info on
102       "PREFIX_MAP" - it may not be properly documented anywhere else...yet!).
103       e.g.
104
105           use Template::Provider::HTTP;
106
107           my $file = Template::Provider( INCLUDE_PATH => [...] );
108           my $http = Template::Provider::HTTP->new(...);
109           my $tt2  = Template->new({
110               LOAD_TEMPLATES => [ $file, $http ],
111               PREFIX_MAP => {
112                   file    => '0',     # file:foo.html
113                   http    => '1',     # http:foo.html
114                   default => '0',     # foo.html => file:foo.html
115               }
116           });
117
118       Now a template specified as:
119
120           [% INCLUDE foo %]
121
122       will be served by the 'file' provider (the default).  Otherwise you can
123       explicitly add a prefix:
124
125           [% INCLUDE file:foo.html %]
126           [% INCLUDE http:foo.html %]
127           [% INCLUDE http://www.xyz.com/tt2/header.tt2 %]
128
129       This same principal can be used to create a DBI template provider.
130       e.g.
131
132           [% INCLUDE dbi:foo.html %]
133
134       Alas, we don't yet have a DBI provider as part of the Template Toolkit.
135       There has been some talk on the mailing list about efforts to develop
136       DBI and/or HTTP providers but as yet no-one has stepped forward to take
137       up the challenge...
138
139       In the mean time, Craig Barrat's post from the mailing list has some
140       useful pointers on how to achieve this using existing modules.  See
141       <http://tt2.org/pipermail/templates/2001-May/000954.html>
142

Miscellaneous

144   How can I find out the name of the main template being processed?
145       The "template" variable contains a reference to the Template::Document
146       object for the main template you're processing (i.e. the one provided
147       as the first argument to the Template process() method).  The "name"
148       method returns its name.
149
150           [% template.name %]     # e.g. index.html
151
152   How can I find out the name of the current template being processed?
153       The "template" variable always references the main template being
154       processed.  So even if you call [% INCLUDE header %], and that calls [%
155       INCLUDE menu %], the "template" variable will be unchanged.
156
157       index.html:
158
159           [% template.name  %]     # index.html
160           [% INCLUDE header %]
161
162       header:
163
164           [% template.name  %]     # index.html
165           [% INCLUDE menu   %]
166
167       menu:
168
169           [% template.name  %]     # index.html
170
171       In contrast, the "component" variable always references the current
172       template being processed.
173
174       index.html
175
176           [% component.name %]     # index.html
177           [% INCLUDE header %]
178
179       header:
180
181           [% component.name %]     # header
182           [% INCLUDE menu   %]
183
184       menu:
185
186           [% component.name  %]     # menu
187
188   How do I print the modification time of the template or component?
189       The "template" and "component" variables reference the main template
190       and the current template being processed (see previous questions).  The
191       "modtime" method returns the modification time of the corresponding
192       template file as a number of seconds since the Unix epoch (00:00:00 GMT
193       1st January 1970).
194
195       This number doesn't mean much to anyone (except perhaps serious Unix
196       geeks) so you'll probably want to use the Date plugin to format it for
197       human consumption.
198
199           [% USE Date %]
200           [% template.name %] last modified [% Date.format(template.modtime) %]
201
202   How can I configure variables on a per-request basis?
203       One easy way to achieve this is to define a single "PRE_PROCESS"
204       template which loads in other configuration files based on variables
205       defined or other conditions.
206
207       For example, my setup usually looks something like this:
208
209           PRE_PROCESS => 'config/main'
210
211       config/main:
212
213           [%  DEFAULT  style   = 'text'
214                        section =  template.section or 'home';
215
216               PROCESS  config/site
217                     +  config/urls
218                     +  config/macros
219                     + "config/style/$style"
220                     + "config/section/$section"
221                     + ...
222           %]
223
224       This allows me to set a single 'style' variable to control which config
225       file gets pre-processed to set my various style options (colours, img
226       paths, etc).  For example:
227
228       config/style/basic:
229
230           [%  style = {
231                   name = style    # save existing 'style' var as 'style.name'
232
233                   # define various other style variables....
234                   col = {
235                       back => '#ffffff'
236                       text => '#000000'
237                           # ...etc...
238                   }
239
240                   logo = {
241                           # ...etc...
242                   }
243
244                   # ...etc...
245               }
246           %]
247
248       Each source template can declare which section it's in via a META
249       directive:
250
251         [% META
252              title   = 'General Information'
253              section = 'info'
254         %]
255         ...
256
257       This controls which section configuration file gets loaded to set
258       various other variables for defining the section title, menu, etc.
259
260       config/section/info:
261
262           [%  section = {
263                   name   = section  # save 'section' var as 'section.name'
264                   title  = 'Information'
265                   menu   = [ ... ]
266                   # ...etc...
267               }
268           %]
269
270       This illustrates the basic principal but you can extend it to perform
271       pretty much any kind of per-document initialisation that you require.
272
273   Why do I get rubbish for my utf-8 templates?
274       First of all, make sure that your template files define a Byte Order
275       Mark <http://en.wikipedia.org/wiki/Byte_Order_Mark>
276
277       If you for some reason don't want to add BOM to your templates, you can
278       force Template to use a particular encoding (e.g. "utf8") for your
279       templates with the "ENCODING" option.
280
281           my $template = Template->new({
282               ENCODING => 'utf8'
283           });
284

Questions About This FAQ

286   Why is this FAQ so short?
287       Because we don't have anyone maintaining it.
288
289   Can I help?
290       Yes please :-)
291
292
293
294perl v5.38.0                      2023-07-21                  Template::FAQ(3)
Impressum