1 #============================================================= -*-perl-*-
9 # Andy Wardley <abw@wardley.org>
12 # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #========================================================================
20 Template::FAQ - Frequently Asked Questions about the Template Toolkit
22 =head1 Template Toolkit Language
24 =head2 Why doesn't [% a = b IF c %] work as expected?
26 There's a limitation in the TT2 parser which means that the following code
27 doesn't work as you might expect:
31 The parser interprets it as an attempt to set C<a> to the result of
36 If you want to set C<a = b> only if C<c> is true, then do this instead:
40 The explicit C<SET> keyword gives the parser the clue it needs to do the
43 NOTE: this will be fixed in TT3
45 =head2 If I'm using TT to write out a TT template, is there a good way to escape [% and %]?
47 You can do something like this:
55 [% stag; 'hello'; etag %]
57 Or you can use the C<TAGS> directive, like so:
60 [- INCLUDE foo -] # is a directive
61 [% INCLUDE foo %] # not a directive
63 =head2 How do I iterate over a hash?
65 This is covered in the L<Template::Manual::VMethods> section of the
66 manual. A list of all the keys that are in the hash can be obtained with the
67 C<keys> virtual method. You can then iterate over that list and by looking up
68 each key in turn get the value.
70 [% FOREACH key = product.keys %]
71 [% key %] => [% product.$key %]
76 =head2 How do I get the Table plugin to order data across rather than down?
78 Order the data into rows:
81 Brooklyn Nantucket Fairfax
84 [% USE table(data, rows=3) %]
86 Then ask for each column
88 [% FOREACH column = table.cols %]
90 And then print each item in the column going across the output rows
92 [% FOREACH item = column %]
96 =head2 Accessing Cookies
98 Jeff Boes E<lt>jboes@nexcerpt.comE<gt> asks:
100 Does anyone have a quick-n-dirty approach to accessing
101 cookies from templates?
103 Jonas Liljegren answers:
107 <p>The value is [% CGI.cookie('cookie_name') | html %]
110 =head1 Extending the Template Toolkit
112 =head2 Can I serve templates from a database?
114 Short answer: yes, Chris Nandor has done this for Slash. You need to
115 subclass L<Template::Provider>. See the mailing list archives for further
118 =head2 Can I fetch templates via http?
120 To do the job properly, you should subclass L<Template::Provider> to
121 C<Template::Provider::HTTP> and use a C<PREFIX_MAP> option to bind the C<http>
122 template prefix to that particular provider (you may want to go digging around
123 in the F<Changes> file around version 2.01 for more info on C<PREFIX_MAP> - it
124 may not be properly documented anywhere else...yet!). e.g.
126 use Template::Provider::HTTP;
128 my $file = Template::Provider( INCLUDE_PATH => [...] );
129 my $http = Template::Provider::HTTP->new(...);
130 my $tt2 = Template->new({
131 LOAD_TEMPLATES => [ $file, $http ],
133 file => '0', # file:foo.html
134 http => '1', # http:foo.html
135 default => '0', # foo.html => file:foo.html
139 Now a template specified as:
143 will be served by the 'file' provider (the default). Otherwise you
144 can explicitly add a prefix:
146 [% INCLUDE file:foo.html %]
147 [% INCLUDE http:foo.html %]
148 [% INCLUDE http://www.xyz.com/tt2/header.tt2 %]
150 This same principal can be used to create a DBI template provider. e.g.
152 [% INCLUDE dbi:foo.html %]
154 Alas, we don't yet have a DBI provider as part of the Template Toolkit. There
155 has been some talk on the mailing list about efforts to develop DBI and/or
156 HTTP providers but as yet no-one has stepped forward to take up the
159 In the mean time, Craig Barrat's post from the mailing list has some useful
160 pointers on how to achieve this using existing modules. See
161 L<http://tt2.org/pipermail/templates/2001-May/000954.html>
165 =head2 How can I find out the name of the main template being processed?
167 The C<template> variable contains a reference to the
168 Template::Document object for the main template you're processing
169 (i.e. the one provided as the first argument to the Template process()
170 method). The C<name> method returns its name.
172 [% template.name %] # e.g. index.html
174 =head2 How can I find out the name of the current template being processed?
176 The C<template> variable always references the I<main> template being processed.
177 So even if you call [% INCLUDE header %], and that calls [% INCLUDE menu %],
178 the C<template> variable will be unchanged.
182 [% template.name %] # index.html
187 [% template.name %] # index.html
192 [% template.name %] # index.html
194 In constrast, the C<component> variable always references the I<current>
195 template being processed.
199 [% component.name %] # index.html
204 [% component.name %] # header
209 [% component.name %] # menu
211 =head2 How do I print the modification time of the template or component?
213 The C<template> and C<component> variables reference the main template
214 and the current template being processed (see previous questions).
215 The C<modtime> method returns the modification time of the
216 corresponding template file as a number of seconds since the Unix
217 epoch (00:00:00 GMT 1st January 1970).
219 This number doesn't mean much to anyone (except perhaps serious Unix
220 geeks) so you'll probably want to use the Date plugin to format it for
224 [% template.name %] last modified [% Date.format(template.modtime) %]
226 =head2 How can I configure variables on a per-request basis?
228 One easy way to achieve this is to define a single C<PRE_PROCESS> template
229 which loads in other configuration files based on variables defined or other
232 For example, my setup usually looks something like this:
234 PRE_PROCESS => 'config/main'
238 [% DEFAULT style = 'text'
239 section = template.section or 'home';
244 + "config/style/$style"
245 + "config/section/$section"
249 This allows me to set a single 'style' variable to control which config
250 file gets pre-processed to set my various style options (colours, img paths,
256 name = style # save existing 'style' var as 'style.name'
258 # define various other style variables....
273 Each source template can declare which section it's in via a META
277 title = 'General Information'
282 This controls which section configuration file gets loaded to set various
283 other variables for defining the section title, menu, etc.
288 name = section # save 'section' var as 'section.name'
289 title = 'Information'
295 This illustrates the basic principal but you can extend it to perform
296 pretty much any kind of per-document initialisation that you require.
298 =head2 Why do I get rubbish for my utf-8 templates?
300 First of all, make sure that your template files define a Byte Order
301 Mark L<http://en.wikipedia.org/wiki/Byte_Order_Mark>
303 If you for some reason don't want to add BOM to your templates, you can
304 force Template to use a particular encoding (e.g. C<utf8>) for your
305 templates with the C<ENCODING> option.
307 my $template = Template->new({
311 =head1 Questions About This FAQ
313 =head2 Why is this FAQ so short?
315 Because we don't have anyone maintaining it.
325 # perl-indent-level: 4
326 # indent-tabs-mode: nil
329 # vim: expandtab shiftwidth=4: