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