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

Extending the Template Toolkit

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

Miscellaneous

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

Questions About This FAQ

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