3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
6
<title>Template::FAQ</title>
7
<link rel="stylesheet" type="text/css" href="../css/blue.css" title="Clear Blue">
8
<link rel="alternate stylesheet" type="text/css" href="../css/orange.css" title="Clear Orange">
9
<link rel="alternate stylesheet" type="text/css" href="../css/green.css" title="Clear Green">
10
<link rel="alternate stylesheet" type="text/css" href="../css/purple.css" title="Clear Purple">
11
<link rel="alternate stylesheet" type="text/css" href="../css/grey.css" title="Clear Grey">
13
<link rel="stylesheet" type="text/css" href="../css/ie6.css" />
15
<link rel="stylesheet" type="text/css" href="/css/print.css" media="print">
16
<script type="text/javascript" src="../js/tt2.js"></script>
17
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
18
<meta name="author" content="Andy Wardley">
23
<a href="../index.html" id="logo" alt="" title="Click for the Home Page"><span class="alt">TT2 Home Page</span></a>
25
<li><a href="..">???</a></li>
26
<li class="last"><a href="../misc/FAQ.html">FAQ</a></li>
28
<div class="controls">
29
<a href="#" class="menu show" onclick="widescreen_off(); return false" title="Show Menu">
30
<span class="about">Click to view the menu. It's very nice.</span>
32
<a href="#" class="menu hide" onclick="widescreen_on(); return false" title="Hide Menu">
33
<span class="about">Click to hide the menu and go all widescreen!</span>
37
<span class="go back">Back<span class="about">Yeah right, I bet you've love to go back, but you can't.</span></span>
38
<span class="go up">Up<span class="about">No, you can't go up. You're silly to even think that you could.</span></span>
39
<span class="go next">Next<span class="about">Since when did going to the next page become so important?</span></span>
42
<h1 class="headline">Template::FAQ</h1>
43
<h2 class="subhead">Frequently Asked Questions about the Template Toolkit</h1>
48
<a href="../index.html" id="logo"></a>
51
<li class="l0 first"><a href="../manual/index.html">Manual</a></li>
52
<li class="l0"><a href="../modules/index.html">Modules</a></li>
53
<li class="l0"><a href="../tools/index.html">Tools</a></li>
54
<li class="l0 last"><a href="../tutorial/index.html">Tutorial</a></li>
56
<div class="foot"></div>
62
<h1 id="contents" onclick="switch_section(this)" title="Click title to show/hide section content.">Contents</h1>
63
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
67
<li class=""><a href="#DESCRIPTION">DESCRIPTION</a></li>
68
<li class=""><a href="#Template_Toolkit_Language">Template Toolkit Language</a></li>
69
<li class="sub"><a href="#section_Why_doesn_t_a_b_IF_c_work_as_expected_">Why doesn't [% a = b IF c %] work as expected?</a></li>
70
<li class="sub"><a href="#section_If_I_m_using_TT_to_write_out_a_TT_template_is_there_a_good_way_to_escape_and_">If I'm using TT to write out a TT template, is there a good way to escape [% and %]?</a></li>
71
<li class="sub"><a href="#section_How_do_I_iterate_over_a_hash_">How do I iterate over a hash?</a></li>
72
<li class=""><a href="#Plugins">Plugins</a></li>
73
<li class="sub"><a href="#section_How_do_I_get_the_Table_plugin_to_order_data_across_rather_than_down_">How do I get the Table plugin to order data across rather than down?</a></li>
74
<li class="sub"><a href="#section_Accessing_Cookies">Accessing Cookies</a></li>
75
<li class=""><a href="#Extending_the_Template_Toolkit">Extending the Template Toolkit</a></li>
76
<li class="sub"><a href="#section_Can_I_serve_templates_from_a_database_">Can I serve templates from a database?</a></li>
77
<li class="sub"><a href="#section_Can_I_fetch_templates_via_http_">Can I fetch templates via http?</a></li>
78
<li class=""><a href="#Miscellaneous">Miscellaneous</a></li>
79
<li class="sub"><a href="#section_How_can_I_find_out_the_name_of_the_main_template_being_processed_">How can I find out the name of the main template being processed?</a></li>
80
<li class="sub"><a href="#section_How_can_I_find_out_the_name_of_the_current_template_being_processed_">How can I find out the name of the current template being processed?</a></li>
81
<li class="sub"><a href="#section_How_do_I_print_the_modification_time_of_the_template_or_component_">How do I print the modification time of the template or component?</a></li>
82
<li class="sub"><a href="#section_How_can_I_configure_variables_on_a_per_request_basis_">How can I configure variables on a per-request basis?</a></li>
83
<li class=""><a href="#AUTHOR">AUTHOR</a></li>
84
<li class=""><a href="#COPYRIGHT">COPYRIGHT</a></li>
93
<h1 id="DESCRIPTION" onclick="switch_section(this)" title="Click title to show/hide section content.">DESCRIPTION</h1>
94
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
100
<div class="section">
102
<h1 id="Template_Toolkit_Language" onclick="switch_section(this)" title="Click title to show/hide section content.">Template Toolkit Language</h1>
103
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
106
<div class="subsection">
108
<h2 id="section_Why_doesn_t_a_b_IF_c_work_as_expected_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">Why doesn't [% a = b IF c %] work as expected?</h2>
109
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
113
Because the parser interprets it as
115
<pre><span class="tt">[% a = (b IF c) %]</span></pre>
119
<pre><span class="tt">[% SET a = b IF c %]</span></pre>
121
</div> <div class="subsection">
123
<h2 id="section_If_I_m_using_TT_to_write_out_a_TT_template_is_there_a_good_way_to_escape_and_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">If I'm using TT to write out a TT template, is there a good way to escape [% and %]?</h2>
124
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
128
You can do this: [% stag = "[\%" etag = "%\]" %] and then: [% stag;
134
<pre><span class="tt">[% TAGS [- -] %]</span>
135
[- INCLUDE foo -] # is a directive
136
<span class="tt">[% INCLUDE foo %]</span> # not a directive, just plain text, passed through</pre>
138
</div> <div class="subsection">
140
<h2 id="section_How_do_I_iterate_over_a_hash_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">How do I iterate over a hash?</h2>
141
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
145
This is covered in the <a
146
href="#section_VMethods">Template::Manual::VMethods</a> section of the
147
manual page. A list of all the keys that are in the hash can be obtained
148
with the 'keys' virtual method. You can then iterate over that list and
149
by looking up each key in turn get the value.
151
<pre><span class="tt">[% FOREACH key = product.keys %]</span>
152
<span class="tt">[% key %]</span> => <span class="tt">[% product.$key %]</span>
153
<span class="tt">[% END %]</span></pre>
158
<div class="section">
160
<h1 id="Plugins" onclick="switch_section(this)" title="Click title to show/hide section content.">Plugins</h1>
161
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
164
<div class="subsection">
166
<h2 id="section_How_do_I_get_the_Table_plugin_to_order_data_across_rather_than_down_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">How do I get the Table plugin to order data across rather than down?</h2>
167
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
171
Order the data into rows:
173
<pre> Steve Karen Jeff
174
Brooklyn Nantucket Fairfax
177
<span class="tt">[% USE table(data, rows=3) %]</span>
179
Then ask for each column
181
<span class="tt">[% FOREACH column = table.cols %]</span>
183
And then print each item in the column going across the output rows
185
<span class="tt">[% FOREACH item = column %]</span>
186
<td><span class="tt">[% item %]</span></td>
187
<span class="tt">[% END %]</span></pre>
189
</div> <div class="subsection">
191
<h2 id="section_Accessing_Cookies" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">Accessing Cookies</h2>
192
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
196
Jeff Boes <jboes@nexcerpt.com> asks:
198
<pre>Does anyone have a quick-n-dirty approach to accessing
199
cookies from templates? </pre>
201
Jonas Liljegren answers:
203
<pre><span class="tt">[% USE CGI %]</span>
205
<p>The value is <span class="tt">[% CGI.cookie('cookie_name') | html %]</span></pre>
210
<div class="section">
212
<h1 id="Extending_the_Template_Toolkit" onclick="switch_section(this)" title="Click title to show/hide section content.">Extending the Template Toolkit</h1>
213
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
216
<div class="subsection">
218
<h2 id="section_Can_I_serve_templates_from_a_database_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">Can I serve templates from a database?</h2>
219
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
223
Short answer: yes, Chris Nandor has done this for Slash. You need to
224
subclass Template::Provider. See the mailing list archives for further
228
</div> <div class="subsection">
230
<h2 id="section_Can_I_fetch_templates_via_http_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">Can I fetch templates via http?</h2>
231
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
235
To do the job properly, you should sublcass Template::Provider to
236
Template::Provider::HTTP and use a PREFIX_MAP option to bind the 'http'
237
template prefix to that particular provider (you may want to go digging
238
around in the <i>Changes</i> file around version 2.01 for more info on
239
PREFIX_MAP - it may not be properly documented anywhere else...yet!).
240
e.g. (untested due to lack of existing HTTP Provider - patches welcome!).
242
<pre>use Template::Provider::HTTP;</pre>
243
<pre>my $file = Template::Provider( INCLUDE_PATH => [...] );
244
my $http = Template::Provider::HTTP->new(...);
245
my $tt2 = Template->new({
246
LOAD_TEMPLATES => [ $file, $http ],
248
file => '0', # file:foo.html
249
http => '1', # http:foo.html
250
default => '0', # foo.html => file:foo.html
254
Now a template specified as:
256
<pre><span class="tt">[% INCLUDE foo %]</span></pre>
258
will be served by the 'file' provider (the default). Otherwise you can
259
explicitly add a prefix:
261
<pre><span class="tt">[% INCLUDE file:foo.html %]</span>
262
<span class="tt">[% INCLUDE http:foo.html %]</span>
263
<span class="tt">[% INCLUDE http://www.xyz.com/tt2/header.tt2 %]</span></pre>
265
This same principal can be used to create a DBI template provider. e.g.
267
<pre><span class="tt">[% INCLUDE dbi:foo.html %]</span></pre>
269
But similarly, alas, we don't yet have a DBI provider as part of the
270
Template Toolkit. There has been some talk on the mailing list about
271
efforts to develop DBI and/or HTTP providers but as yet no-one has
272
stepped forward to take up the challenge...
275
In the mean time, Craig's post from the mailing list has some useful
276
pointers on how to acheive this using existing modules:
278
<pre>To: Adam Theo <adamtheo@theoretic.com>
279
From: Craig Barratt <craig@arraycomm.com>
280
Date: Fri, 18 May 2001 17:06:59 -0700
282
> i was wondering if there is anyway to fetch a file using http:// or
283
> ftp:// and include that?
285
Here's one way. Set the LOAD_PERL option:
289
my $template = Template->new({
292
$template->process("example.tt", { stdout => *STDOUT })
293
|| die $template->error();
295
and then use LWP::UserAgent and HTTP::Request:
298
USE ua = LWP.UserAgent;
299
ua.proxy("http", "http://your_proxy/");
300
USE req = HTTP.Request("GET", "http://www.cpan.org");
301
ua.request(req).content;
304
For FTP use Net::FTP:
307
USE ftp = Net.FTP("ftp.cpan.org");
308
x = ftp.login("anonymous", "me@here.there");
310
x = ftp.get("welcome.msg", stdout);
314
Normally ftp.get would write the file into the current directory.
315
Instead we pass stdout as a second argument so that it is written
316
to stdout. We set stdout to STDOUT in the variables we pass to
324
<div class="section">
326
<h1 id="Miscellaneous" onclick="switch_section(this)" title="Click title to show/hide section content.">Miscellaneous</h1>
327
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
330
<div class="subsection">
332
<h2 id="section_How_can_I_find_out_the_name_of_the_main_template_being_processed_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">How can I find out the name of the main template being processed?</h2>
333
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
337
The <code>template</code> variable contains a reference to the
338
Template::Document object for the main template you're processing (i.e.
339
the one provided as the first argument to the Template process() method).
340
The <code>name</code> method returns its name.
342
<pre><span class="tt">[% template.name %]</span> # e.g. index.html</pre>
344
</div> <div class="subsection">
346
<h2 id="section_How_can_I_find_out_the_name_of_the_current_template_being_processed_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">How can I find out the name of the current template being processed?</h2>
347
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
351
The <code>template</code> variable always references the <i>main</i>
352
template being processed. So even if you call [% INCLUDE header %], and
353
that calls [% INCLUDE menu %], the <code>template</code> variable will be
359
<pre><span class="tt">[% template.name %]</span> # index.html
360
<span class="tt">[% INCLUDE header %]</span></pre>
364
<pre><span class="tt">[% template.name %]</span> # index.html
365
<span class="tt">[% INCLUDE menu %]</span></pre>
369
<pre><span class="tt">[% template.name %]</span> # index.html</pre>
371
In constrast, the <code>component</code> variable always references the
372
<i>current</i> template being processed.
377
<pre><span class="tt">[% component.name %]</span> # index.html
378
<span class="tt">[% INCLUDE header %]</span></pre>
382
<pre><span class="tt">[% component.name %]</span> # header
383
<span class="tt">[% INCLUDE menu %]</span></pre>
387
<pre><span class="tt">[% component.name %]</span> # menu</pre>
389
</div> <div class="subsection">
391
<h2 id="section_How_do_I_print_the_modification_time_of_the_template_or_component_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">How do I print the modification time of the template or component?</h2>
392
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
396
The <code>template</code> and <code>component</code> variables reference
397
the main template and the current template being processed (see previous
398
questions). The <code>modtime</code> method returns the modification time
399
of the corresponding template file as a number of seconds since the Unix
400
epoch (00:00:00 GMT 1st January 1970).
403
This number doesn't mean much to anyone (except perhaps serious Unix
404
geeks) so you'll probably want to use the Date plugin to format it for
407
<pre><span class="tt">[% USE Date %]</span></pre>
408
<pre><span class="tt">[% template.name %]</span> last modified <span class="tt">[% Date.format(template.modtime) %]</span></pre>
410
</div> <div class="subsection">
412
<h2 id="section_How_can_I_configure_variables_on_a_per_request_basis_" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">How can I configure variables on a per-request basis?</h2>
413
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
417
One easy way to acheive this is to define a single PRE_PROCESS template
418
which loads in other configuration files based on variables defined or
422
For example, my setup usually looks something like this:
424
<pre>PRE_PROCESS => 'config/main'</pre>
428
<pre>[% DEFAULT style = 'text'
429
section = template.section or 'home';</pre>
430
<pre> PROCESS config/site
433
+ "config/style/$style"
434
+ "config/section/$section"
438
This allows me to set a single 'style' variable to control which config
439
file gets pre-processed to set my various style options (colours, img
440
paths, etc). For example:
446
name = style # save existing 'style' var as 'style.name'</pre>
447
<pre># define various other style variables....
460
Each source template can declare which section it's in via a META
463
<pre><span class="tt">[% META
464
title = 'General Information'
469
This controls which section configuration file gets loaded to set various
470
other variables for defining the section title, menu, etc.
475
<pre><span class="tt">[% section = {
476
name = section # save 'section' var as 'section.name'
477
title = 'Information'
483
This illustrates the basic principal but you can extend it to perform
484
pretty much any kind of per-document initialisation that you require.
490
<div class="section">
492
<h1 id="AUTHOR" onclick="switch_section(this)" title="Click title to show/hide section content.">AUTHOR</h1>
493
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
497
Andy Wardley <abw@wardley.org> <a
498
href="http://wardley.org/">http://wardley.org/</a>
502
<div class="section">
504
<h1 id="COPYRIGHT" onclick="switch_section(this)" title="Click title to show/hide section content.">COPYRIGHT</h1>
505
<a href="#body" class="top" title="Back up to the top of the page" >Top</a>
509
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
512
This module is free software; you can redistribute it and/or modify it
513
under the same terms as Perl itself.
520
<div class="pageinfo">
521
/misc/FAQ.html last modified 10:38:14 01-Jun-2007
526
<a href="http://opensource.org/" class="osi"></a>
527
<div class="controls">
529
<span class="go back">Back<span class="about"></span></span>
530
<span class="go up">Up<span class="about"></span></span>
531
<span class="go next">Next<span class="about"></span></span>
534
<div class="copyright">
535
Copyright © 1996-2007 <a href="http://wardley.org/">Andy Wardley</a>. All Rights Reserved.
537
<div class="licence">
538
The <a href="http://template-toolkit.org/">Template Toolkit</a> is <a href="http://opensource.org/">Open Source</a> software.
539
You can redistribute and/or modify it under the terms of the <a href="http://www.opensource.org/licenses/gpl-license.php">GNU Public Licence</a>
540
or the <a href="http://www.opensource.org/licenses/artistic-license.php">Perl Artistic Licence</a>.
545
<li class="first"><a href="#" class="blue" onclick="set_style('Clear Blue')"></a></li>
546
<li><a href="#" class="orange" onclick="set_style('Clear Orange')"></a></li>
547
<li><a href="#" class="green" onclick="set_style('Clear Green')"></a></li>
548
<li><a href="#" class="purple" onclick="set_style('Clear Purple')"></a></li>
549
<li><a href="#" class="grey" onclick="set_style('Clear Grey')"></a></li>