~ubuntu-branches/ubuntu/quantal/libtemplate-perl/quantal

« back to all changes in this revision

Viewing changes to html/old/manual/Directives.html

  • Committer: Package Import Robot
  • Author(s): Benjamin Mako Hill
  • Date: 2012-04-08 19:06:29 UTC
  • mfrom: (0.7.1) (0.5.2) (5.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20120408190629-wbcbs2ea39mex6lt
Tags: 2.24-1
* New upstream release (Closes: #664561)
* Bump Standards-Version to 3.9.3
* Changed to the short description to mention the term "Template
  Toolkit". (LP: #688836)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
 
4
<html>
 
5
  <head>
 
6
    <title>Template::Manual::Directives</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">
 
12
    <!--[if IE 6]>
 
13
    <link rel="stylesheet" type="text/css" href="../css/ie6.css" />
 
14
    <![endif]-->
 
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">
 
19
  </head>
 
20
  <body id="body"> 
 
21
    <div id="layout">
 
22
        <div id="header">
 
23
          <a href="../index.html" id="logo" alt="" title="Click for the Home Page"><span class="alt">TT2 Home Page</span></a>
 
24
          <ul id="trail">
 
25
            <li><a href="../manual/index.html">Manual</a></li>
 
26
            <li class="last"><a href="../manual/Directives.html">Directives</a></li>
 
27
          </ul>
 
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>
 
31
            </a>
 
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>
 
34
            </a>
 
35
          
 
36
          <div class="pager">
 
37
            <a href="../manual/Syntax.html" title="Template::Manual::Syntax" class="go back">Back<span class="about"><h4>Template::Manual::Syntax</h4>Directive syntax, structure and semantics</span></a>
 
38
            <a href="../manual/index.html" title="Template::Manual" class="go up">Up<span class="about"><h4>Template::Manual</h4>Template Toolkit User Manual</span></a>
 
39
            <a href="../manual/Variables.html" title="Template::Manual::Variables" class="go next">Next<span class="about"><h4>Template::Manual::Variables</h4>Template variables and code bindings</span></a>
 
40
          </div>
 
41
          </div>
 
42
          <h1 class="headline">Template::Manual::Directives</h1>
 
43
          <h2 class="subhead">Template directives</h1>
 
44
        
 
45
        </div>
 
46
        <div id="page">
 
47
          <div id="sidebar">
 
48
            <a href="../index.html" id="logo"></a>
 
49
            <div id="menu">
 
50
              <ul class="menu">
 
51
                <li class="l0 first"><a href="../manual/index.html" class="warm">Manual</a></li>
 
52
                <li class="l1"><a href="../manual/Intro.html">Intro</a></li>
 
53
                <li class="l1"><a href="../manual/Syntax.html">Syntax</a></li>
 
54
                <li class="l1"><a href="../manual/Directives.html" class="warm">Directives</a></li>
 
55
                <li class="l1"><a href="../manual/Variables.html">Variables</a></li>
 
56
                <li class="l1"><a href="../manual/VMethods.html">VMethods</a></li>
 
57
                <li class="l1"><a href="../manual/Config.html">Config</a></li>
 
58
                <li class="l1"><a href="../manual/Filters.html">Filters</a></li>
 
59
                <li class="l1"><a href="../manual/Plugins.html">Plugins</a></li>
 
60
                <li class="l1"><a href="../manual/Internals.html">Internals</a></li>
 
61
                <li class="l1"><a href="../manual/Views.html">Views</a></li>
 
62
                <li class="l1"><a href="../manual/Credits.html">Credits</a></li>
 
63
                <li class="l0"><a href="../modules/index.html">Modules</a></li>
 
64
                <li class="l0"><a href="../tools/index.html">Tools</a></li>
 
65
                <li class="l0 last"><a href="../tutorial/index.html">Tutorial</a></li>
 
66
              </ul>
 
67
              <div class="foot"></div>
 
68
            </div>
 
69
          </div>
 
70
          <div id="content">
 
71
          <div class="section">
 
72
            <div class="head">
 
73
              <h1 id="contents" onclick="switch_section(this)" title="Click title to show/hide section content.">Contents</h1>
 
74
              <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
75
            </div>
 
76
            <div class="body">
 
77
              <ul class="toc">
 
78
                  <li class=""><a href="#Accessing_and_Updating_Template_Variables">Accessing and Updating Template Variables</a></li>
 
79
                  <li class="sub"><a href="#section_GET">GET</a></li>
 
80
                  <li class="sub"><a href="#section_CALL">CALL</a></li>
 
81
                  <li class="sub"><a href="#section_SET">SET</a></li>
 
82
                  <li class="sub"><a href="#section_DEFAULT">DEFAULT</a></li>
 
83
                  <li class=""><a href="#Processing_Template_Files_and_Blocks">Processing Template Files and Blocks</a></li>
 
84
                  <li class="sub"><a href="#section_INSERT">INSERT</a></li>
 
85
                  <li class="sub"><a href="#section_INCLUDE">INCLUDE</a></li>
 
86
                  <li class="sub"><a href="#section_PROCESS">PROCESS</a></li>
 
87
                  <li class="sub"><a href="#section_WRAPPER">WRAPPER</a></li>
 
88
                  <li class="sub"><a href="#section_BLOCK">BLOCK</a></li>
 
89
                  <li class=""><a href="#Conditional_Processing">Conditional Processing</a></li>
 
90
                  <li class="sub"><a href="#section_IF_UNLESS_ELSIF_ELSE">IF / UNLESS / ELSIF / ELSE</a></li>
 
91
                  <li class="sub"><a href="#section_SWITCH_CASE">SWITCH / CASE</a></li>
 
92
                  <li class=""><a href="#Loop_Processing">Loop Processing</a></li>
 
93
                  <li class="sub"><a href="#section_FOREACH">FOREACH</a></li>
 
94
                  <li class="sub"><a href="#section_WHILE">WHILE</a></li>
 
95
                  <li class=""><a href="#Filters_Plugins_Macros_and_Perl">Filters, Plugins, Macros and Perl</a></li>
 
96
                  <li class="sub"><a href="#section_FILTER">FILTER</a></li>
 
97
                  <li class="sub"><a href="#section_USE">USE</a></li>
 
98
                  <li class="sub"><a href="#section_MACRO">MACRO</a></li>
 
99
                  <li class="sub"><a href="#section_PERL">PERL</a></li>
 
100
                  <li class="sub"><a href="#section_RAWPERL">RAWPERL</a></li>
 
101
                  <li class=""><a href="#Exception_Handling_and_Flow_Control">Exception Handling and Flow Control</a></li>
 
102
                  <li class="sub"><a href="#section_TRY_THROW_CATCH_FINAL">TRY / THROW / CATCH / FINAL</a></li>
 
103
                  <li class="sub"><a href="#section_NEXT">NEXT</a></li>
 
104
                  <li class="sub"><a href="#section_LAST">LAST</a></li>
 
105
                  <li class="sub"><a href="#section_RETURN">RETURN</a></li>
 
106
                  <li class="sub"><a href="#section_STOP">STOP</a></li>
 
107
                  <li class="sub"><a href="#section_CLEAR">CLEAR</a></li>
 
108
                  <li class=""><a href="#Miscellaneous">Miscellaneous</a></li>
 
109
                  <li class="sub"><a href="#section_META">META</a></li>
 
110
                  <li class="sub"><a href="#section_TAGS">TAGS</a></li>
 
111
                  <li class="sub"><a href="#section_DEBUG">DEBUG</a></li>
 
112
              
 
113
              </ul>
 
114
            </div>
 
115
          </div>
 
116
          
 
117
                <div class="pod">
 
118
            <div class="section">
 
119
              <div class="head">
 
120
                <h1 id="Accessing_and_Updating_Template_Variables" onclick="switch_section(this)" title="Click title to show/hide section content.">Accessing and Updating Template Variables</h1>
 
121
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
122
              </div>
 
123
              <div class="body">
 
124
                <div class="subsection">
 
125
                  <div class="head">
 
126
                    <h2 id="section_GET" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">GET</h2>
 
127
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
128
                  </div>
 
129
                  <div class="body">
 
130
                    <p>
 
131
                          The <code>GET</code> directive retrieves and outputs the value of the
 
132
                          named variable.
 
133
                        </p>
 
134
                        <pre>[% GET foo %]</pre>
 
135
                        <p>
 
136
                          The <code>GET</code> keyword is optional. A variable can be specified in
 
137
                          a directive tag by itself.
 
138
                        </p>
 
139
                        <pre>[% foo %]</pre>
 
140
                        <p>
 
141
                          The variable can have an unlimited number of elements, each separated by
 
142
                          a dot. Each element can have arguments specified within parentheses.
 
143
                        </p>
 
144
                        <pre>[% foo %]
 
145
[% bar.baz %]
 
146
[% biz.baz(10) %]
 
147
...etc...</pre>
 
148
                        <p>
 
149
                          See <a href="../manual/Variables.html">Template::Manual::Variables</a> for a full discussion on template
 
150
                          variables.
 
151
                        </p>
 
152
                        <p>
 
153
                          You can also specify expressions using the logical (<code>and</code>,
 
154
                          <code>or</code>, <code>not</code>, <code>?</code>, <code>:</code>) and
 
155
                          mathematic operators (<code>+</code>, <code>-</code>, <code>*</code>,
 
156
                          <code>/</code>, <code>%</code>, <code>mod</code>, <code>div</code>).
 
157
                        </p>
 
158
                        <pre>[% template.title or default.title %]
 
159
 
 
160
[% score * 100 %]
 
161
 
 
162
[% order.nitems ? checkout(order.total) : 'no items' %]</pre>
 
163
                        <p>
 
164
                          The <code>div</code> operator returns the integer result of division.
 
165
                          Both <code>%</code> and <code>mod</code> return the modulus (i.e.
 
166
                          remainder) of division.
 
167
                        </p>
 
168
                        <pre>[% 15 / 6 %]            # 2.5
 
169
[% 15 div 6 %]          # 2
 
170
[% 15 mod 6 %]          # 3</pre>
 
171
                  </div>
 
172
                </div>    <div class="subsection">
 
173
                  <div class="head">
 
174
                    <h2 id="section_CALL" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">CALL</h2>
 
175
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
176
                  </div>
 
177
                  <div class="body">
 
178
                    <p>
 
179
                          The <code>CALL</code> directive is similar to <code>GET</code> in
 
180
                          evaluating the variable named, but doesn't print the result returned.
 
181
                          This can be useful when a variable is bound to a sub-routine or object
 
182
                          method which you want to call but aren't interested in the value
 
183
                          returned.
 
184
                        </p>
 
185
                        <pre>[% CALL dbi.disconnect %]
 
186
 
 
187
[% CALL inc_page_counter(page_count) %]</pre>
 
188
                  </div>
 
189
                </div>    <div class="subsection">
 
190
                  <div class="head">
 
191
                    <h2 id="section_SET" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">SET</h2>
 
192
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
193
                  </div>
 
194
                  <div class="body">
 
195
                    <p>
 
196
                          The <code>SET</code> directive allows you to assign new values to
 
197
                          existing variables or create new temporary variables.
 
198
                        </p>
 
199
                        <pre>[% SET title = 'Hello World' %]</pre>
 
200
                        <p>
 
201
                          The <code>SET</code> keyword is also optional. [% title = 'Hello World'
 
202
                          %]
 
203
                        </p>
 
204
                        <p>
 
205
                          Variables may be assigned the values of other variables, unquoted numbers
 
206
                          (2.718), literal text ('single quotes') or quoted text ("double quotes").
 
207
                          In the latter case, any variable references within the text will be
 
208
                          interpolated when the string is evaluated. Variables should be prefixed
 
209
                          by <code>$</code>, using curly braces to explicitly scope the variable
 
210
                          name where necessary.
 
211
                        </p>
 
212
                        <pre>[% foo  = 'Foo'  %]               # literal value 'Foo'
 
213
[% bar  =  foo   %]               # value of variable 'foo'
 
214
[% cost = '$100' %]               # literal value '$100'
 
215
[% item = "$bar: ${cost}.00" %]   # value "Foo: $100.00"</pre>
 
216
                        <p>
 
217
                          Multiple variables may be assigned in the same directive and are
 
218
                          evaluated in the order specified. Thus, the above could have been
 
219
                          written:
 
220
                        </p>
 
221
                        <pre>[% foo  = 'Foo'
 
222
   bar  = foo
 
223
   cost = '$100'
 
224
   item = "$bar: ${cost}.00"
 
225
%]</pre>
 
226
                        <p>
 
227
                          Simple expressions can also be used, as per <code>GET</code>.
 
228
                        </p>
 
229
                        <pre>[% ten    = 10 
 
230
   twenty = 20
 
231
   thirty = twenty + ten
 
232
   forty  = 2 * twenty 
 
233
   fifty  = 100 div 2
 
234
   six    = twenty mod 7
 
235
%]</pre>
 
236
                        <p>
 
237
                          You can concatenate strings together using the <code>' _ '</code>
 
238
                          operator. In Perl 5, the <code>.</code> is used for string concatenation,
 
239
                          but in Perl 6, as in the Template Toolkit, the <code>.</code> will be
 
240
                          used as the method calling operator and <code>' _ '</code> will be used
 
241
                          for string concatenation. Note that the operator must be specified with
 
242
                          surrounding whitespace which, as Larry says, is construed as a feature:
 
243
                        </p>
 
244
                        <pre>[% copyright = '(C) Copyright' _ year _ ' ' _ author %]</pre>
 
245
                        <p>
 
246
                          You can, of course, achieve a similar effect with double quoted string
 
247
                          interpolation.
 
248
                        </p>
 
249
                        <pre>[% copyright = "(C) Copyright $year $author" %]</pre>
 
250
                  </div>
 
251
                </div>    <div class="subsection">
 
252
                  <div class="head">
 
253
                    <h2 id="section_DEFAULT" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">DEFAULT</h2>
 
254
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
255
                  </div>
 
256
                  <div class="body">
 
257
                    <p>
 
258
                          The <code>DEFAULT</code> directive is similar to <code>SET</code> but
 
259
                          only updates variables that are currently undefined or have no "true"
 
260
                          value (in the Perl sense).
 
261
                        </p>
 
262
                        <pre>[% DEFAULT
 
263
    name = 'John Doe'
 
264
    id   = 'jdoe'
 
265
%]</pre>
 
266
                        <p>
 
267
                          This can be particularly useful in common template components to ensure
 
268
                          that some sensible default are provided for otherwise undefined
 
269
                          variables.
 
270
                        </p>
 
271
                        <pre>[% DEFAULT 
 
272
   title = 'Hello World'
 
273
   bgcol = '#ffffff'
 
274
%]
 
275
&lt;html&gt;
 
276
  &lt;head&gt;
 
277
    &lt;title&gt;[% title %]&lt;/title&gt;
 
278
  &lt;/head&gt;
 
279
  &lt;body bgcolor="[% bgcol %]"&gt;
 
280
    ...etc...</pre>
 
281
                  </div>
 
282
                </div>
 
283
              </div>
 
284
            </div>
 
285
            <div class="section">
 
286
              <div class="head">
 
287
                <h1 id="Processing_Template_Files_and_Blocks" onclick="switch_section(this)" title="Click title to show/hide section content.">Processing Template Files and Blocks</h1>
 
288
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
289
              </div>
 
290
              <div class="body">
 
291
                <div class="subsection">
 
292
                  <div class="head">
 
293
                    <h2 id="section_INSERT" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">INSERT</h2>
 
294
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
295
                  </div>
 
296
                  <div class="body">
 
297
                    <p>
 
298
                          The <code>INSERT</code> directive is used to insert the contents of an
 
299
                          external file at the current position.
 
300
                        </p>
 
301
                        <pre>[% INSERT myfile %]</pre>
 
302
                        <p>
 
303
                          No attempt to parse or process the file is made. The contents, possibly
 
304
                          including any embedded template directives, are inserted intact.
 
305
                        </p>
 
306
                        <p>
 
307
                          The filename specified should be relative to one of the
 
308
                          <code>INCLUDE_PATH</code> directories. Absolute (i.e. starting with
 
309
                          <code>/</code>) and relative (i.e. starting with <code>.</code>)
 
310
                          filenames may be used if the <code>ABSOLUTE</code> and
 
311
                          <code>RELATIVE</code> options are set, respectively. Both these options
 
312
                          are disabled by default.
 
313
                        </p>
 
314
                        <pre>my $template = Template-&gt;new({
 
315
    INCLUDE_PATH =&gt; '/here:/there',
 
316
});
 
317
 
 
318
$template-&gt;process('myfile');</pre>
 
319
                        <p>
 
320
                          <i>myfile</i>:
 
321
                        </p>
 
322
                        <pre>[% INSERT foo %]            # looks for /here/foo then /there/foo
 
323
[% INSERT /etc/passwd %]    # file error: ABSOLUTE not set
 
324
[% INSERT ../secret %]      # file error: RELATIVE not set</pre>
 
325
                        <p>
 
326
                          For convenience, the filename does not need to be quoted as long as it
 
327
                          contains only alphanumeric characters, underscores, dots or forward
 
328
                          slashes. Names containing any other characters should be quoted.
 
329
                        </p>
 
330
                        <pre>[% INSERT misc/legalese.txt            %]
 
331
[% INSERT 'dos98/Program Files/stupid' %]</pre>
 
332
                        <p>
 
333
                          To evaluate a variable to specify a filename, you should explicitly
 
334
                          prefix it with a <code>$</code> or use double-quoted string
 
335
                          interpolation.
 
336
                        </p>
 
337
                        <pre>[% language = 'en'
 
338
   legalese = 'misc/legalese.txt' 
 
339
%]
 
340
 
 
341
[% INSERT $legalese %]              # misc/legalese.txt
 
342
[% INSERT "$language/$legalese" %]  # en/misc/legalese.txt</pre>
 
343
                        <p>
 
344
                          Multiple files can be specified using <code>+</code> as a delimiter. All
 
345
                          files should be unquoted names or quoted strings. Any variables should be
 
346
                          interpolated into double-quoted strings.
 
347
                        </p>
 
348
                        <pre>[% INSERT legalese.txt + warning.txt %]
 
349
[% INSERT  "$legalese" + warning.txt %]  # requires quoting</pre>
 
350
                  </div>
 
351
                </div>    <div class="subsection">
 
352
                  <div class="head">
 
353
                    <h2 id="section_INCLUDE" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">INCLUDE</h2>
 
354
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
355
                  </div>
 
356
                  <div class="body">
 
357
                    <p>
 
358
                          The <code>INCLUDE</code> directive is used to process and include the
 
359
                          output of another template file or block.
 
360
                        </p>
 
361
                        <pre>[% INCLUDE header %]</pre>
 
362
                        <p>
 
363
                          If a <code>BLOCK</code> of the specified name is defined in the same
 
364
                          file, or in a file from which the current template has been called (i.e.
 
365
                          a parent template) then it will be used in preference to any file of the
 
366
                          same name.
 
367
                        </p>
 
368
                        <pre>[% INCLUDE table %]     # uses BLOCK defined below
 
369
 
 
370
[% BLOCK table %]
 
371
   &lt;table&gt;
 
372
     ...
 
373
   &lt;/table&gt;
 
374
[% END %]</pre>
 
375
                        <p>
 
376
                          If a <code>BLOCK</code> definition is not currently visible then the
 
377
                          template name should be a file relative to one of the
 
378
                          <code>INCLUDE_PATH</code> directories, or an absolute or relative file
 
379
                          name if the <code>ABSOLUTE</code>/<code>RELATIVE</code> options are
 
380
                          appropriately enabled. The <code>INCLUDE</code> directive automatically
 
381
                          quotes the filename specified, as per <code>INSERT</code> described
 
382
                          above. When a variable contains the name of the template for the
 
383
                          <code>INCLUDE</code> directive, it should be explicitly prefixed by
 
384
                          <code>$</code> or double-quoted
 
385
                        </p>
 
386
                        <pre>[% myheader = 'my/misc/header' %]
 
387
[% INCLUDE   myheader  %]           # 'myheader'
 
388
[% INCLUDE  $myheader  %]           # 'my/misc/header'
 
389
[% INCLUDE "$myheader" %]           # 'my/misc/header'</pre>
 
390
                        <p>
 
391
                          Any template directives embedded within the file will be processed
 
392
                          accordingly. All variables currently defined will be visible and
 
393
                          accessible from within the included template.
 
394
                        </p>
 
395
                        <pre>[% title = 'Hello World' %]
 
396
[% INCLUDE header %]
 
397
&lt;body&gt;
 
398
...</pre>
 
399
                        <p>
 
400
                          <i>header</i>:
 
401
                        </p>
 
402
                        <pre>&lt;html&gt;
 
403
&lt;title&gt;[% title %]&lt;/title&gt;</pre>
 
404
                        <p>
 
405
                          output:
 
406
                        </p>
 
407
                        <pre>&lt;html&gt;
 
408
&lt;title&gt;Hello World&lt;/title&gt;
 
409
&lt;body&gt;
 
410
...</pre>
 
411
                        <p>
 
412
                          Local variable definitions may be specified after the template name,
 
413
                          temporarily masking any existing variables. Insignificant whitespace is
 
414
                          ignored within directives so you can add variable definitions on the same
 
415
                          line, the next line or split across several line with comments
 
416
                          interspersed, if you prefer.
 
417
                        </p>
 
418
                        <pre>[% INCLUDE table %]
 
419
 
 
420
[% INCLUDE table title="Active Projects" %]
 
421
 
 
422
[% INCLUDE table 
 
423
     title   = "Active Projects" 
 
424
     bgcolor = "#80ff00"    # chartreuse
 
425
     border  = 2
 
426
%]</pre>
 
427
                        <p>
 
428
                          The <code>INCLUDE</code> directive localises (i.e. copies) all variables
 
429
                          before processing the template. Any changes made within the included
 
430
                          template will not affect variables in the including template.
 
431
                        </p>
 
432
                        <pre>[% foo = 10 %]
 
433
 
 
434
foo is originally [% foo %]
 
435
[% INCLUDE bar %]
 
436
foo is still [% foo %]
 
437
 
 
438
[% BLOCK bar %]
 
439
   foo was [% foo %]
 
440
   [% foo = 20 %]
 
441
   foo is now [% foo %]
 
442
[% END %]</pre>
 
443
                        <p>
 
444
                          output:
 
445
                        </p>
 
446
                        <pre>foo is originally 10
 
447
   foo was 10
 
448
   foo is now 20
 
449
foo is still 10</pre>
 
450
                        <p>
 
451
                          Technical Note: the localisation of the stash (that is, the process by
 
452
                          which variables are copied before an <code>INCLUDE</code> to prevent
 
453
                          being overwritten) is only skin deep. The top-level variable namespace
 
454
                          (hash) is copied, but no attempt is made to perform a deep-copy of other
 
455
                          structures (hashes, arrays, objects, etc.) Therefore, a <code>foo</code>
 
456
                          variable referencing a hash will be copied to create a new
 
457
                          <code>foo</code> variable but which points to the same hash array. Thus,
 
458
                          if you update compound variables (e.g. <code>foo.bar</code>) then you
 
459
                          will change the original copy, regardless of any stash localisation. If
 
460
                          you're not worried about preserving variable values, or you trust the
 
461
                          templates you're including then you might prefer to use the
 
462
                          <code>PROCESS</code> directive which is faster by virtue of not
 
463
                          performing any localisation.
 
464
                        </p>
 
465
                        <p>
 
466
                          You can specify dotted variables as "local" variables to an
 
467
                          <code>INCLUDE</code> directive. However, be aware that because of the
 
468
                          localisation issues explained above (if you skipped the previous
 
469
                          Technical Note above then you might want to go back and read it or skip
 
470
                          this section too), the variables might not actualy be "local". If the
 
471
                          first element of the variable name already references a hash array then
 
472
                          the variable update will affect the original variable.
 
473
                        </p>
 
474
                        <pre>[% foo = {
 
475
       bar = 'Baz'
 
476
   }
 
477
%]
 
478
 
 
479
[% INCLUDE somefile foo.bar='Boz' %]
 
480
 
 
481
[% foo.bar %]           # Boz</pre>
 
482
                        <p>
 
483
                          This behaviour can be a little unpredictable (and may well be improved
 
484
                          upon in a future version). If you know what you're doing with it and
 
485
                          you're sure that the variables in question are defined (nor not) as you
 
486
                          expect them to be, then you can rely on this feature to implement some
 
487
                          powerful "global" data sharing techniques. Otherwise, you might prefer to
 
488
                          steer well clear and always pass simple (undotted) variables as
 
489
                          parameters to <code>INCLUDE</code> and other similar directives.
 
490
                        </p>
 
491
                        <p>
 
492
                          If you want to process several templates in one go then you can specify
 
493
                          each of their names (quoted or unquoted names only, no unquoted
 
494
                          <code>$variables</code>) joined together by <code>+</code>. The
 
495
                          <code>INCLUDE</code> directive will then process them in order.
 
496
                        </p>
 
497
                        <pre>[% INCLUDE html/header + "site/$header" + site/menu
 
498
     title = "My Groovy Web Site"
 
499
%]</pre>
 
500
                        <p>
 
501
                          The variable stash is localised once and then the templates specified are
 
502
                          processed in order, all within that same variable context. This makes it
 
503
                          slightly faster than specifying several separate <code>INCLUDE</code>
 
504
                          directives (because you only clone the variable stash once instead of n
 
505
                          times), but not quite as "safe" because any variable changes in the first
 
506
                          file will be visible in the second, third and so on. This might be what
 
507
                          you want, of course, but then again, it might not.
 
508
                        </p>
 
509
                  </div>
 
510
                </div>    <div class="subsection">
 
511
                  <div class="head">
 
512
                    <h2 id="section_PROCESS" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">PROCESS</h2>
 
513
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
514
                  </div>
 
515
                  <div class="body">
 
516
                    <p>
 
517
                          The PROCESS directive is similar to <code>INCLUDE</code> but does not
 
518
                          perform any localisation of variables before processing the template. Any
 
519
                          changes made to variables within the included template will be visible in
 
520
                          the including template.
 
521
                        </p>
 
522
                        <pre>[% foo = 10 %]
 
523
 
 
524
foo is [% foo %]
 
525
[% PROCESS bar %]
 
526
foo is [% foo %]
 
527
 
 
528
[% BLOCK bar %]
 
529
   [% foo = 20 %]
 
530
   changed foo to [% foo %]
 
531
[% END %]</pre>
 
532
                        <p>
 
533
                          output:
 
534
                        </p>
 
535
                        <pre>foo is 10
 
536
   changed foo to 20
 
537
foo is 20</pre>
 
538
                        <p>
 
539
                          Parameters may be specified in the <code>PROCESS</code> directive, but
 
540
                          these too will become visible changes to current variable values.
 
541
                        </p>
 
542
                        <pre>[% foo = 10 %]
 
543
foo is [% foo %]
 
544
[% PROCESS bar
 
545
   foo = 20 
 
546
%]
 
547
foo is [% foo %]
 
548
 
 
549
[% BLOCK bar %]
 
550
   this is bar, foo is [% foo %]
 
551
[% END %]</pre>
 
552
                        <p>
 
553
                          output:
 
554
                        </p>
 
555
                        <pre>foo is 10
 
556
   this is bar, foo is 20
 
557
foo is 20</pre>
 
558
                        <p>
 
559
                          The <code>PROCESS</code> directive is slightly faster than
 
560
                          <code>INCLUDE</code> because it avoids the need to localise (i.e. copy)
 
561
                          the variable stash before processing the template. As with
 
562
                          <code>INSERT</code> and <code>INCLUDE</code>, the first parameter does
 
563
                          not need to be quoted as long as it contains only alphanumeric
 
564
                          characters, underscores, periods or forward slashes. A <code>$</code>
 
565
                          prefix can be used to explicitly indicate a variable which should be
 
566
                          interpolated to provide the template name:
 
567
                        </p>
 
568
                        <pre>[% myheader = 'my/misc/header' %]
 
569
[% PROCESS  myheader %]              # 'myheader'
 
570
[% PROCESS $myheader %]              # 'my/misc/header'</pre>
 
571
                        <p>
 
572
                          As with <code>INCLUDE</code>, multiple templates can be specified,
 
573
                          delimited by <code>+</code>, and are processed in order.
 
574
                        </p>
 
575
                        <pre>[% PROCESS html/header + my/header %]</pre>
 
576
                  </div>
 
577
                </div>    <div class="subsection">
 
578
                  <div class="head">
 
579
                    <h2 id="section_WRAPPER" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">WRAPPER</h2>
 
580
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
581
                  </div>
 
582
                  <div class="body">
 
583
                    <p>
 
584
                          It's not unusual to find yourself adding common headers and footers to
 
585
                          pages or sub-sections within a page. Something like this:
 
586
                        </p>
 
587
                        <pre>[% INCLUDE section/header
 
588
   title = 'Quantum Mechanics'
 
589
%]
 
590
   Quantum mechanics is a very interesting subject wish 
 
591
   should prove easy for the layman to fully comprehend.
 
592
[% INCLUDE section/footer %]
 
593
 
 
594
[% INCLUDE section/header
 
595
   title = 'Desktop Nuclear Fusion for under $50'
 
596
%]
 
597
   This describes a simple device which generates significant 
 
598
   sustainable electrical power from common tap water by process 
 
599
   of nuclear fusion.
 
600
[% INCLUDE section/footer %]</pre>
 
601
                        <p>
 
602
                          The individual template components being included might look like these:
 
603
                        </p>
 
604
                        <p>
 
605
                          section/header:
 
606
                        </p>
 
607
                        <pre>&lt;p&gt;
 
608
&lt;h2&gt;[% title %]&lt;/h2&gt;</pre>
 
609
                        <p>
 
610
                          section/footer:
 
611
                        </p>
 
612
                        <pre>&lt;/p&gt;</pre>
 
613
                        <p>
 
614
                          The <code>WRAPPER</code> directive provides a way of simplifying this a
 
615
                          little. It encloses a block up to a matching <code>END</code> directive,
 
616
                          which is first processed to generate some output. This is then passed to
 
617
                          the named template file or <code>BLOCK</code> as the <code>content</code>
 
618
                          variable.
 
619
                        </p>
 
620
                        <pre>[% WRAPPER section
 
621
   title = 'Quantum Mechanics'
 
622
%]
 
623
   Quantum mechanics is a very interesting subject wish 
 
624
   should prove easy for the layman to fully comprehend.
 
625
[% END %]
 
626
 
 
627
[% WRAPPER section
 
628
   title = 'Desktop Nuclear Fusion for under $50'
 
629
%]
 
630
   This describes a simple device which generates significant 
 
631
   sustainable electrical power from common tap water by process 
 
632
   of nuclear fusion.
 
633
[% END %]</pre>
 
634
                        <p>
 
635
                          The single 'section' template can then be defined as:
 
636
                        </p>
 
637
                        <pre>&lt;h2&gt;[% title %]&lt;/h2&gt;
 
638
&lt;p&gt;
 
639
  [% content %]
 
640
&lt;/p&gt;</pre>
 
641
                        <p>
 
642
                          Like other block directives, it can be used in side-effect notation:
 
643
                        </p>
 
644
                        <pre>[% INSERT legalese.txt WRAPPER big_bold_table %]</pre>
 
645
                        <p>
 
646
                          It's also possible to specify multiple templates to a
 
647
                          <code>WRAPPER</code> directive. The specification order indicates
 
648
                          outermost to innermost wrapper templates. For example, given the
 
649
                          following template block definitions:
 
650
                        </p>
 
651
                        <pre>[% BLOCK bold   %]&lt;b&gt;[% content %]&lt;/b&gt;[% END %]
 
652
[% BLOCK italic %]&lt;i&gt;[% content %]&lt;/i&gt;[% END %]</pre>
 
653
                        <p>
 
654
                          the directive
 
655
                        </p>
 
656
                        <pre>[% WRAPPER bold+italic %]Hello World[% END %]</pre>
 
657
                        <p>
 
658
                          would generate the following output:
 
659
                        </p>
 
660
                        <pre>&lt;b&gt;&lt;i&gt;Hello World&lt;/i&gt;&lt;/b&gt;</pre>
 
661
                  </div>
 
662
                </div>    <div class="subsection">
 
663
                  <div class="head">
 
664
                    <h2 id="section_BLOCK" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">BLOCK</h2>
 
665
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
666
                  </div>
 
667
                  <div class="body">
 
668
                    <p>
 
669
                          The <code>BLOCK</code>...<code>END</code> construct can be used to define
 
670
                          template component blocks which can be processed with the
 
671
                          <code>INCLUDE</code>, <code>PROCESS</code> and <code>WRAPPER</code>
 
672
                          directives.
 
673
                        </p>
 
674
                        <pre>[% BLOCK tabrow %]
 
675
&lt;tr&gt;
 
676
  &lt;td&gt;[% name %]&lt;td&gt;
 
677
  &lt;td&gt;[% email %]&lt;/td&gt;
 
678
&lt;/tr&gt;
 
679
[% END %]
 
680
 
 
681
&lt;table&gt;
 
682
  [% PROCESS tabrow  name='Fred'  email='fred@nowhere.com' %]
 
683
  [% PROCESS tabrow  name='Alan'  email='alan@nowhere.com' %]
 
684
&lt;/table&gt;</pre>
 
685
                        <p>
 
686
                          A <code>BLOCK</code> definition can be used before it is defined, as long
 
687
                          as the definition resides in the same file. The block definition itself
 
688
                          does not generate any output.
 
689
                        </p>
 
690
                        <pre>[% PROCESS tmpblk %]
 
691
 
 
692
[% BLOCK tmpblk %] This is OK [% END %]</pre>
 
693
                        <p>
 
694
                          You can use an anonymous <code>BLOCK</code> to capture the output of a
 
695
                          template fragment.
 
696
                        </p>
 
697
                        <pre>[% julius = BLOCK %]
 
698
   And Caesar's spirit, ranging for revenge,
 
699
   With Ate by his side come hot from hell,
 
700
   Shall in these confines with a monarch's voice
 
701
   Cry  'Havoc', and let slip the dogs of war;
 
702
   That this foul deed shall smell above the earth
 
703
   With carrion men, groaning for burial.
 
704
[% END %]</pre>
 
705
                        <p>
 
706
                          Like a named block, it can contain any other template directives which
 
707
                          are processed when the block is defined. The output generated by the
 
708
                          block is then assigned to the variable <code>julius</code>.
 
709
                        </p>
 
710
                        <p>
 
711
                          Anonymous <code>BLOCK</code>s can also be used to define block macros.
 
712
                          The enclosing block is processed each time the macro is called.
 
713
                        </p>
 
714
                        <pre>[% MACRO locate BLOCK %]
 
715
   The [% animal %] sat on the [% place %].
 
716
[% END %]
 
717
 
 
718
[% locate(animal='cat', place='mat') %]    # The cat sat on the mat
 
719
[% locate(animal='dog', place='log') %]    # The dog sat on the log</pre>
 
720
                  </div>
 
721
                </div>
 
722
              </div>
 
723
            </div>
 
724
            <div class="section">
 
725
              <div class="head">
 
726
                <h1 id="Conditional_Processing" onclick="switch_section(this)" title="Click title to show/hide section content.">Conditional Processing</h1>
 
727
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
728
              </div>
 
729
              <div class="body">
 
730
                <div class="subsection">
 
731
                  <div class="head">
 
732
                    <h2 id="section_IF_UNLESS_ELSIF_ELSE" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">IF / UNLESS / ELSIF / ELSE</h2>
 
733
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
734
                  </div>
 
735
                  <div class="body">
 
736
                    <p>
 
737
                          The <code>IF</code> and <code>UNLESS</code> directives can be used to
 
738
                          process or ignore a block based on some run-time condition.
 
739
                        </p>
 
740
                        <pre>[% IF frames %]
 
741
   [% INCLUDE frameset %]
 
742
[% END %]
 
743
 
 
744
[% UNLESS text_mode %]
 
745
   [% INCLUDE biglogo %]
 
746
[% END %]</pre>
 
747
                        <p>
 
748
                          Multiple conditions may be joined with <code>ELSIF</code> and/or
 
749
                          <code>ELSE</code> blocks.
 
750
                        </p>
 
751
                        <pre>[% IF age &lt; 10 %]
 
752
   Hello [% name %], does your mother know you're 
 
753
   using her AOL account?
 
754
[% ELSIF age &lt; 18 %]
 
755
   Sorry, you're not old enough to enter 
 
756
   (and too dumb to lie about your age)
 
757
[% ELSE %]
 
758
   Welcome [% name %].
 
759
[% END %]</pre>
 
760
                        <p>
 
761
                          The following conditional and boolean operators may be used:
 
762
                        </p>
 
763
                        <pre>== != &lt; &lt;= &gt; &gt;= &amp;&amp; || ! and or not</pre>
 
764
                        <p>
 
765
                          Conditions may be arbitrarily complex and are evaluated with the same
 
766
                          precedence as in Perl. Parenthesis may be used to explicitly determine
 
767
                          evaluation order.
 
768
                        </p>
 
769
                        <pre># ridiculously contrived complex example
 
770
[% IF (name == 'admin' || uid &lt;= 0) &amp;&amp; mode == 'debug' %]
 
771
   I'm confused.
 
772
[% ELSIF more &gt; less %]
 
773
   That's more or less correct.
 
774
[% END %]</pre>
 
775
                        <p>
 
776
                          The <code>and</code>, <code>or</code> and <code>not</code> operator are
 
777
                          provided as aliases for <code>&amp;&amp;</code>, <code>||</code> and
 
778
                          <code>!</code>, respectively. Unlike Perl, which treats <code>and</code>,
 
779
                          <code>or</code> and <code>not</code> as separate, lower-precedence
 
780
                          versions of the other operators, the Template Toolkit performs a
 
781
                          straightforward substitution of <code>and</code> for
 
782
                          <code>&amp;&amp;</code>, and so on. That means that <code>and</code>,
 
783
                          <code>or</code> and <code>not</code> have the same operator precedence as
 
784
                          <code>&amp;&amp;</code>, <code>||</code> and <code>!</code>.
 
785
                        </p>
 
786
                  </div>
 
787
                </div>    <div class="subsection">
 
788
                  <div class="head">
 
789
                    <h2 id="section_SWITCH_CASE" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">SWITCH / CASE</h2>
 
790
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
791
                  </div>
 
792
                  <div class="body">
 
793
                    <p>
 
794
                          The <code>SWITCH</code> / <code>CASE</code> construct can be used to
 
795
                          perform a multi-way conditional test. The <code>SWITCH</code> directive
 
796
                          expects an expression which is first evaluated and then compared against
 
797
                          each CASE statement in turn. Each <code>CASE</code> directive should
 
798
                          contain a single value or a list of values which should match.
 
799
                          <code>CASE</code> may also be left blank or written as <code>[% CASE
 
800
                          DEFAULT %]</code> to specify a default match. Only one <code>CASE</code>
 
801
                          matches, there is no drop-through between <code>CASE</code> statements.
 
802
                        </p>
 
803
                        <pre>[% SWITCH myvar %]
 
804
[%   CASE value1 %]
 
805
       ...
 
806
[%   CASE [ value2 value3 ] %]   # multiple values
 
807
       ...
 
808
[%   CASE myhash.keys %]         # ditto
 
809
       ...
 
810
[%   CASE %]                     # default
 
811
       ...
 
812
[% END %]</pre>
 
813
                  </div>
 
814
                </div>
 
815
              </div>
 
816
            </div>
 
817
            <div class="section">
 
818
              <div class="head">
 
819
                <h1 id="Loop_Processing" onclick="switch_section(this)" title="Click title to show/hide section content.">Loop Processing</h1>
 
820
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
821
              </div>
 
822
              <div class="body">
 
823
                <div class="subsection">
 
824
                  <div class="head">
 
825
                    <h2 id="section_FOREACH" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">FOREACH</h2>
 
826
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
827
                  </div>
 
828
                  <div class="body">
 
829
                    <p>
 
830
                          The <code>FOREACH</code> directive will iterate through the items in a
 
831
                          list, processing the enclosed block for each one.
 
832
                        </p>
 
833
                        <pre>my $vars = {
 
834
    foo   =&gt; 'Foo',
 
835
    items =&gt; [ 'one', 'two', 'three' ],
 
836
};
 
837
 
 
838
template:</pre>
 
839
                        <pre>Things:
 
840
[% FOREACH thing = [ foo 'Bar' "$foo Baz" ] %]
 
841
   * [% thing %]
 
842
[% END %]
 
843
 
 
844
Items:
 
845
[% FOREACH i IN items %]
 
846
   * [% i %]
 
847
[% END %]
 
848
 
 
849
Stuff:
 
850
[% stuff = [ foo "$foo Bar" ] %]
 
851
[% FOREACH s IN stuff %]
 
852
   * [% s %]
 
853
[% END %]</pre>
 
854
                        <p>
 
855
                          output:
 
856
                        </p>
 
857
                        <pre>Things:
 
858
  * Foo
 
859
  * Bar
 
860
  * Foo Baz
 
861
 
 
862
Items:
 
863
  * one
 
864
  * two
 
865
  * three
 
866
 
 
867
Stuff:
 
868
  * Foo
 
869
  * Foo Bar</pre>
 
870
                        <p>
 
871
                          You can use also use <code>IN</code> instead of <code>=</code> if you
 
872
                          prefer.
 
873
                        </p>
 
874
                        <pre>[% FOREACH crook IN government %]</pre>
 
875
                        <p>
 
876
                          When the <code>FOREACH</code> directive is used without specifying a
 
877
                          target variable, any iterated values which are hash references will be
 
878
                          automatically imported.
 
879
                        </p>
 
880
                        <pre>[% userlist = [
 
881
    { id =&gt; 'tom',   name =&gt; 'Thomas'  },
 
882
    { id =&gt; 'dick',  name =&gt; 'Richard'  },
 
883
    { id =&gt; 'larry', name =&gt; 'Lawrence' },
 
884
   ]
 
885
%]
 
886
 
 
887
[% FOREACH user IN userlist %]
 
888
   [% user.id %] [% user.name %]
 
889
[% END %]</pre>
 
890
                        <p>
 
891
                          short form:
 
892
                        </p>
 
893
                        <pre>[% FOREACH userlist %]
 
894
   [% id %] [% name %]
 
895
[% END %]</pre>
 
896
                        <p>
 
897
                          Note that this particular usage creates a localised variable context to
 
898
                          prevent the imported hash keys from overwriting any existing variables.
 
899
                          The imported definitions and any other variables defined in such a
 
900
                          <code>FOREACH</code> loop will be lost at the end of the loop, when the
 
901
                          previous context and variable values are restored.
 
902
                        </p>
 
903
                        <p>
 
904
                          However, under normal operation, the loop variable remains in scope after
 
905
                          the <code>FOREACH</code> loop has ended (caveat: overwriting any variable
 
906
                          previously in scope). This is useful as the loop variable is secretly an
 
907
                          iterator object (see below) and can be used to analyse the last entry
 
908
                          processed by the loop.
 
909
                        </p>
 
910
                        <p>
 
911
                          The <code>FOREACH</code> directive can also be used to iterate through
 
912
                          the entries in a hash array. Each entry in the hash is returned in sorted
 
913
                          order (based on the key) as a hash array containing 'key' and 'value'
 
914
                          items.
 
915
                        </p>
 
916
                        <pre>[% users = {
 
917
     tom   =&gt; 'Thomas',
 
918
     dick  =&gt; 'Richard',
 
919
     larry =&gt; 'Lawrence',
 
920
   }
 
921
%]
 
922
 
 
923
[% FOREACH u IN users %]
 
924
   * [% u.key %] : [% u.value %]
 
925
[% END %]</pre>
 
926
                        <p>
 
927
                          Output:
 
928
                        </p>
 
929
                        <pre>* dick : Richard
 
930
* larry : Lawrence
 
931
* tom : Thomas      </pre>
 
932
                        <p>
 
933
                          The <code>NEXT</code> directive starts the next iteration in the
 
934
                          <code>FOREACH</code> loop.
 
935
                        </p>
 
936
                        <pre>[% FOREACH user IN userlist %]
 
937
   [% NEXT IF user.isguest %]
 
938
   Name: [% user.name %]    Email: [% user.email %]
 
939
[% END %]</pre>
 
940
                        <p>
 
941
                          The <code>LAST</code> directive can be used to prematurely exit the loop.
 
942
                          <code>BREAK</code> is also provided as an alias for <code>LAST</code>.
 
943
                        </p>
 
944
                        <pre>[% FOREACH match IN results.nsort('score').reverse %]
 
945
   [% LAST IF match.score &lt; 50 %]
 
946
   [% match.score %] : [% match.url %]
 
947
[% END %]</pre>
 
948
                        <p>
 
949
                          The <code>FOREACH</code> directive is implemented using the <a href="../modules/Template/Iterator.html">Template::Iterator</a> module. A
 
950
                          reference to the iterator object for a <code>FOREACH</code> directive is
 
951
                          implicitly available in the <code>loop</code> variable. The following
 
952
                          methods can be called on the <code>loop</code> iterator.
 
953
                        </p>
 
954
                        <pre>size()      number of elements in the list
 
955
max()       index number of last element (size - 1)
 
956
index()     index of current iteration from 0 to max()
 
957
count()     iteration counter from 1 to size() (i.e. index() + 1)
 
958
first()     true if the current iteration is the first
 
959
last()      true if the current iteration is the last
 
960
prev()      return the previous item in the list
 
961
next()      return the next item in the list</pre>
 
962
                        <p>
 
963
                          See <a href="../modules/Template/Iterator.html">Template::Iterator</a> for further details.
 
964
                        </p>
 
965
                        <p>
 
966
                          Example:
 
967
                        </p>
 
968
                        <pre>[% FOREACH item IN [ 'foo', 'bar', 'baz' ] -%]
 
969
   [%- "&lt;ul&gt;\n" IF loop.first %]
 
970
   &lt;li&gt;[% loop.count %]/[% loop.size %]: [% item %]
 
971
   [%- "&lt;/ul&gt;\n" IF loop.last %]
 
972
[% END %]</pre>
 
973
                        <p>
 
974
                          Output:
 
975
                        </p>
 
976
                        <pre>&lt;ul&gt;
 
977
&lt;li&gt;1/3: foo
 
978
&lt;li&gt;2/3: bar
 
979
&lt;li&gt;3/3: baz
 
980
&lt;/ul&gt;</pre>
 
981
                        <p>
 
982
                          Nested loops will work as expected, with the <code>loop</code> variable
 
983
                          correctly referencing the innermost loop and being restored to any
 
984
                          previous value (i.e. an outer loop) at the end of the loop.
 
985
                        </p>
 
986
                        <pre>[% FOREACH group IN grouplist;
 
987
     # loop =&gt; group iterator
 
988
     "Groups:\n" IF loop.first;
 
989
 
 
990
     FOREACH user IN group.userlist;
 
991
        # loop =&gt; user iterator
 
992
        "$loop.count: $user.name\n";
 
993
     END;
 
994
 
 
995
     # loop =&gt; group iterator
 
996
     "End of Groups\n" IF loop.last;
 
997
   END 
 
998
%]</pre>
 
999
                        <p>
 
1000
                          The <code>iterator</code> plugin can also be used to explicitly create an
 
1001
                          iterator object. This can be useful within nested loops where you need to
 
1002
                          keep a reference to the outer iterator within the inner loop. The
 
1003
                          iterator plugin effectively allows you to create an iterator by a name
 
1004
                          other than <code>loop</code>. See <a href="../modules/Template/Plugin/Iterator.html">Template::Plugin::Iterator</a> for further details.
 
1005
                        </p>
 
1006
                        <pre>[% USE giter = iterator(grouplist) %]
 
1007
 
 
1008
[% FOREACH group IN giter %]
 
1009
   [% FOREACH user IN group.userlist %]
 
1010
         user #[% loop.count %] in
 
1011
         group [% giter.count %] is
 
1012
         named [% user.name %]
 
1013
   [% END %]
 
1014
[% END %]</pre>
 
1015
                  </div>
 
1016
                </div>    <div class="subsection">
 
1017
                  <div class="head">
 
1018
                    <h2 id="section_WHILE" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">WHILE</h2>
 
1019
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
1020
                  </div>
 
1021
                  <div class="body">
 
1022
                    <p>
 
1023
                          The <code>WHILE</code> directive can be used to repeatedly process a
 
1024
                          template block while a conditional expression evaluates true. The
 
1025
                          expression may be arbitrarily complex as per <code>IF</code> /
 
1026
                          <code>UNLESS</code>.
 
1027
                        </p>
 
1028
                        <pre>[% WHILE total &lt; 100 %]
 
1029
   ...
 
1030
   [% total = calculate_new_total %]
 
1031
[% END %]</pre>
 
1032
                        <p>
 
1033
                          An assignment can be enclosed in parenthesis to evaluate the assigned
 
1034
                          value.
 
1035
                        </p>
 
1036
                        <pre>[% WHILE (user = get_next_user_record) %]
 
1037
   [% user.name %]
 
1038
[% END %]</pre>
 
1039
                        <p>
 
1040
                          The <code>NEXT</code> directive can be used to start the next iteration
 
1041
                          of a <code>WHILE</code> loop and <code>BREAK</code> can be used to exit
 
1042
                          the loop, both as per <code>FOREACH</code>.
 
1043
                        </p>
 
1044
                        <p>
 
1045
                          The Template Toolkit uses a failsafe counter to prevent runaway
 
1046
                          <code>WHILE</code> loops which would otherwise never terminate. If the
 
1047
                          loop exceeds 1000 iterations then an <code>undef</code> exception will be
 
1048
                          thrown, reporting the error:
 
1049
                        </p>
 
1050
                        <pre>WHILE loop terminated (&gt; 1000 iterations)</pre>
 
1051
                        <p>
 
1052
                          The <code>$Template::Directive::WHILE_MAX</code> variable controls this
 
1053
                          behaviour and can be set to a higher value if necessary.
 
1054
                        </p>
 
1055
                  </div>
 
1056
                </div>
 
1057
              </div>
 
1058
            </div>
 
1059
            <div class="section">
 
1060
              <div class="head">
 
1061
                <h1 id="Filters_Plugins_Macros_and_Perl" onclick="switch_section(this)" title="Click title to show/hide section content.">Filters, Plugins, Macros and Perl</h1>
 
1062
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
1063
              </div>
 
1064
              <div class="body">
 
1065
                <div class="subsection">
 
1066
                  <div class="head">
 
1067
                    <h2 id="section_FILTER" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">FILTER</h2>
 
1068
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
1069
                  </div>
 
1070
                  <div class="body">
 
1071
                    <p>
 
1072
                          The <code>FILTER</code> directive can be used to post-process the output
 
1073
                          of a block. A number of standard filters are provided with the Template
 
1074
                          Toolkit. The <code>html</code> filter, for example, escapes the '&lt;',
 
1075
                          '&gt;' and '&amp;' characters to prevent them from being interpreted as
 
1076
                          HTML tags or entity reference markers.
 
1077
                        </p>
 
1078
                        <pre>[% FILTER html %]
 
1079
   HTML text may have &lt; and &gt; characters embedded
 
1080
   which you want converted to the correct HTML entities.
 
1081
[% END %]</pre>
 
1082
                        <p>
 
1083
                          output:
 
1084
                        </p>
 
1085
                        <pre>HTML text may have &amp;lt; and &amp;gt; characters embedded
 
1086
which you want converted to the correct HTML entities.</pre>
 
1087
                        <p>
 
1088
                          The <code>FILTER</code> directive can also follow various other non-block
 
1089
                          directives. For example:
 
1090
                        </p>
 
1091
                        <pre>[% INCLUDE mytext FILTER html %]</pre>
 
1092
                        <p>
 
1093
                          The <code>|</code> character can also be used as an alias for
 
1094
                          <code>FILTER</code>.
 
1095
                        </p>
 
1096
                        <pre>[% INCLUDE mytext | html %]</pre>
 
1097
                        <p>
 
1098
                          Multiple filters can be chained together and will be called in sequence.
 
1099
                        </p>
 
1100
                        <pre>[% INCLUDE mytext FILTER html FILTER html_para %]</pre>
 
1101
                        <p>
 
1102
                          or
 
1103
                        </p>
 
1104
                        <pre>[% INCLUDE mytext | html | html_para %]</pre>
 
1105
                        <p>
 
1106
                          Filters come in two flavours, known as 'static' or 'dynamic'. A static
 
1107
                          filter is a simple subroutine which accepts a text string as the only
 
1108
                          argument and returns the modified text. The <code>html</code> filter is
 
1109
                          an example of a static filter, implemented as:
 
1110
                        </p>
 
1111
                        <pre>sub html_filter {
 
1112
    my $text = shift;
 
1113
    for ($text) {
 
1114
        s/&amp;/&amp;amp;/g;
 
1115
        s/&lt;/&amp;lt;/g;
 
1116
        s/&gt;/&amp;gt;/g;
 
1117
    }
 
1118
    return $text;
 
1119
}</pre>
 
1120
                        <p>
 
1121
                          Dynamic filters can accept arguments which are specified when the filter
 
1122
                          is called from a template. The <code>repeat</code> filter is such an
 
1123
                          example, accepting a numerical argument which specifies the number of
 
1124
                          times that the input text should be repeated.
 
1125
                        </p>
 
1126
                        <pre>[% FILTER repeat(3) %]blah [% END %]</pre>
 
1127
                        <p>
 
1128
                          output:
 
1129
                        </p>
 
1130
                        <pre>blah blah blah</pre>
 
1131
                        <p>
 
1132
                          These are implemented as filter 'factories'. The factory subroutine is
 
1133
                          passed a reference to the current <a href="../modules/Template/Context.html">Template::Context</a> object along
 
1134
                          with any additional arguments specified. It should then return a
 
1135
                          subroutine reference (e.g. a closure) which implements the filter. The
 
1136
                          <code>repeat</code> filter factory is implemented like this:
 
1137
                        </p>
 
1138
                        <pre>sub repeat_filter_factory {
 
1139
    my ($context, $iter) = @_;
 
1140
    $iter = 1 unless defined $iter;
 
1141
 
 
1142
    return sub {
 
1143
        my $text = shift;
 
1144
        $text = '' unless defined $text;
 
1145
        return join('\n', $text) x $iter;
 
1146
    }
 
1147
}</pre>
 
1148
                        <p>
 
1149
                          The <code>FILTERS</code> option, described in <a href="../manual/Config.html">Template::Manual::Config</a>,
 
1150
                          allows custom filters to be defined when a Template object is
 
1151
                          instantiated. The <a
 
1152
                          href="#method_define_filter">Template::Context#define_filter()</a> method
 
1153
                          allows further filters to be defined at any time.
 
1154
                        </p>
 
1155
                        <p>
 
1156
                          When using a filter, it is possible to assign an alias to it for further
 
1157
                          use. This is most useful for dynamic filters that you want to re-use with
 
1158
                          the same configuration.
 
1159
                        </p>
 
1160
                        <pre>[% FILTER echo = repeat(2) %]
 
1161
Is there anybody out there?
 
1162
[% END %]
 
1163
 
 
1164
[% FILTER echo %]
 
1165
Mother, should I build a wall?
 
1166
[% END %]</pre>
 
1167
                        <p>
 
1168
                          Output:
 
1169
                        </p>
 
1170
                        <pre>Is there anybody out there?
 
1171
Is there anybody out there?</pre>
 
1172
                        <pre>Mother, should I build a wall?
 
1173
Mother, should I build a wall?</pre>
 
1174
                        <p>
 
1175
                          The <code>FILTER</code> directive automatically quotes the name of the
 
1176
                          filter. As with <code>INCLUDE</code> et al, you can use a variable to
 
1177
                          provide the name of the filter, prefixed by <code>$</code>.
 
1178
                        </p>
 
1179
                        <pre>[% myfilter = 'html' %]
 
1180
[% FILTER $myfilter %]      # same as [% FILTER html %]
 
1181
   ...
 
1182
[% END %]</pre>
 
1183
                        <p>
 
1184
                          A template variable can also be used to define a static filter
 
1185
                          subroutine. However, the Template Toolkit will automatically call any
 
1186
                          subroutine bound to a variable and use the value returned. Thus, the
 
1187
                          above example could be implemented as:
 
1188
                        </p>
 
1189
                        <pre>my $vars = {
 
1190
    myfilter =&gt; sub { return 'html' },
 
1191
};</pre>
 
1192
                        <p>
 
1193
                          template:
 
1194
                        </p>
 
1195
                        <pre>[% FILTER $myfilter %]      # same as [% FILTER html %]
 
1196
   ...
 
1197
[% END %]</pre>
 
1198
                        <p>
 
1199
                          To define a template variable that evaluates to a subroutine reference
 
1200
                          that can be used by the <code>FILTER</code> directive, you should create
 
1201
                          a subroutine that, when called automatically by the Template Toolkit,
 
1202
                          returns another subroutine reference which can then be used to perform
 
1203
                          the filter operation. Note that only static filters can be implemented in
 
1204
                          this way.
 
1205
                        </p>
 
1206
                        <pre>my $vars = {
 
1207
    myfilter =&gt; sub { \&amp;my_filter_sub },
 
1208
};
 
1209
 
 
1210
sub my_filter_sub {
 
1211
    my $text = shift;
 
1212
    # do something
 
1213
    return $text;
 
1214
}</pre>
 
1215
                        <p>
 
1216
                          template:
 
1217
                        </p>
 
1218
                        <pre>[% FILTER $myfilter %]
 
1219
   ...
 
1220
[% END %]</pre>
 
1221
                        <p>
 
1222
                          Alternately, you can bless a subroutine reference into a class (any class
 
1223
                          will do) to fool the Template Toolkit into thinking it's an object rather
 
1224
                          than a subroutine. This will then bypass the automatic
 
1225
                          "call-a-subroutine-to-return-a-value" magic.
 
1226
                        </p>
 
1227
                        <pre>my $vars = {
 
1228
    myfilter =&gt; bless(\&amp;my_filter_sub, 'anything_you_like'),
 
1229
};</pre>
 
1230
                        <p>
 
1231
                          template:
 
1232
                        </p>
 
1233
                        <pre>[% FILTER $myfilter %]          
 
1234
   ...
 
1235
[% END %]</pre>
 
1236
                        <p>
 
1237
                          Filters bound to template variables remain local to the variable context
 
1238
                          in which they are defined. That is, if you define a filter in a
 
1239
                          <code>PERL</code> block within a template that is loaded via
 
1240
                          <code>INCLUDE</code>, then the filter definition will only exist until
 
1241
                          the end of that template when the stash is delocalised, restoring the
 
1242
                          previous variable state. If you want to define a filter which persists
 
1243
                          for the lifetime of the processor, or define additional dynamic filter
 
1244
                          factories, then you can call the <a
 
1245
                          href="#method_define_filter">Template::Context#define_filter()</a> method
 
1246
                          on the current <a href="../modules/Template/Context.html">Template::Context</a> object.
 
1247
                        </p>
 
1248
                        <p>
 
1249
                          See <a href="../manual/Filters.html">Template::Manual::Filters</a> for a complete list of available
 
1250
                          filters, their descriptions and examples of use.
 
1251
                        </p>
 
1252
                  </div>
 
1253
                </div>    <div class="subsection">
 
1254
                  <div class="head">
 
1255
                    <h2 id="section_USE" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">USE</h2>
 
1256
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
1257
                  </div>
 
1258
                  <div class="body">
 
1259
                    <p>
 
1260
                          The <code>USE</code> directive can be used to load and initialise
 
1261
                          "plugin" extension modules.
 
1262
                        </p>
 
1263
                        <pre>[% USE myplugin %]</pre>
 
1264
                        <p>
 
1265
                          A plugin is a regular Perl module that conforms to a particular
 
1266
                          object-oriented interface, allowing it to be loaded into and used
 
1267
                          automatically by the Template Toolkit. For details of this interface and
 
1268
                          information on writing plugins, consult <a href="../modules/Template/Plugin.html">Template::Plugin</a>.
 
1269
                        </p>
 
1270
                        <p>
 
1271
                          A number of standard plugins are included with the Template Toolkit (see
 
1272
                          below and <a href="../manual/Plugins.html">Template::Manual::Plugins</a>). The names of these standard plugins
 
1273
                          are case insensitive.
 
1274
                        </p>
 
1275
                        <pre>[% USE CGI   %]        # =&gt; Template::Plugin::CGI
 
1276
[% USE Cgi   %]        # =&gt; Template::Plugin::CGI
 
1277
[% USE cgi   %]        # =&gt; Template::Plugin::CGI</pre>
 
1278
                        <p>
 
1279
                          You can also define further plugins using the <code>PLUGINS</code>
 
1280
                          option.
 
1281
                        </p>
 
1282
                        <pre>my $tt = Template-&gt;new({
 
1283
    PLUGINS =&gt; {
 
1284
        foo =&gt; 'My::Plugin::Foo',
 
1285
        bar =&gt; 'My::Plugin::Bar',
 
1286
    },
 
1287
});</pre>
 
1288
                        <p>
 
1289
                          The recommended convention is to specify these plugin names in lower
 
1290
                          case. The Template Toolkit first looks for an exact case-sensitive match
 
1291
                          and then tries the lower case conversion of the name specified.
 
1292
                        </p>
 
1293
                        <pre>[% USE Foo %]      # look for 'Foo' then 'foo'</pre>
 
1294
                        <p>
 
1295
                          If you define all your <code>PLUGINS</code> with lower case names then
 
1296
                          they will be located regardless of how the user specifies the name in the
 
1297
                          <code>USE</code> directive. If, on the other hand, you define your
 
1298
                          <code>PLUGINS</code> with upper or mixed case names then the name
 
1299
                          specified in the <code>USE</code> directive must match the case exactly.
 
1300
                        </p>
 
1301
                        <p>
 
1302
                          If the plugin isn't defined in either the standard plugins
 
1303
                          (<code>$Template::Plugins::STD_PLUGINS</code>) or via the
 
1304
                          <code>PLUGINS</code> option, then the <code>PLUGIN_BASE</code> is
 
1305
                          searched.
 
1306
                        </p>
 
1307
                        <p>
 
1308
                          In this case the plugin name <i>is</i> case-sensitive. It is appended to
 
1309
                          each of the <code>PLUGIN_BASE</code> module namespaces in turn (default:
 
1310
                          <code>Template::Plugin</code>) to construct a full module name which it
 
1311
                          attempts to locate and load. Any periods, '<code>.</code>', in the name
 
1312
                          will be converted to '<code>::</code>'.
 
1313
                        </p>
 
1314
                        <pre>[% USE MyPlugin %]     #  =&gt; Template::Plugin::MyPlugin
 
1315
[% USE Foo.Bar  %]     #  =&gt; Template::Plugin::Foo::Bar</pre>
 
1316
                        <p>
 
1317
                          The <code>LOAD_PERL</code> option (disabled by default) provides a
 
1318
                          further way by which external Perl modules may be loaded. If a regular
 
1319
                          Perl module (i.e. not a <code>Template::Plugin::*</code> or other module
 
1320
                          relative to some <code>PLUGIN_BASE</code>) supports an object-oriented
 
1321
                          interface and a <code>new()</code> constructor then it can be loaded and
 
1322
                          instantiated automatically. The following trivial example shows how the
 
1323
                          IO::File module might be used.
 
1324
                        </p>
 
1325
                        <pre>[% USE file = IO.File('/tmp/mydata') %]
 
1326
 
 
1327
[% WHILE (line = file.getline) %]
 
1328
   &lt;!-- [% line %] --&gt;
 
1329
[% END %]</pre>
 
1330
                        <p>
 
1331
                          Any additional parameters supplied in parenthesis after the plugin name
 
1332
                          will be also be passed to the <code>new()</code> constructor. A reference
 
1333
                          to the current <a href="../modules/Template/Context.html">Template::Context</a> object is passed as the first parameter.
 
1334
                        </p>
 
1335
                        <pre>[% USE MyPlugin('foo', 123) %]</pre>
 
1336
                        <p>
 
1337
                          equivalent to:
 
1338
                        </p>
 
1339
                        <pre>Template::Plugin::MyPlugin-&gt;new($context, 'foo', 123);</pre>
 
1340
                        <p>
 
1341
                          The only exception to this is when a module is loaded via the
 
1342
                          <code>LOAD_PERL</code> option. In this case the <code>$context</code>
 
1343
                          reference is <i>not</i> passed to the <code>new()</code> constructor.
 
1344
                          This is based on the assumption that the module is a regular Perl module
 
1345
                          rather than a Template Toolkit plugin so isn't expecting a context
 
1346
                          reference and wouldn't know what to do with it anyway.
 
1347
                        </p>
 
1348
                        <p>
 
1349
                          Named parameters may also be specified. These are collated into a hash
 
1350
                          which is passed by reference as the last parameter to the constructor, as
 
1351
                          per the general code calling interface.
 
1352
                        </p>
 
1353
                        <pre>[% USE url('/cgi-bin/foo', mode='submit', debug=1) %]</pre>
 
1354
                        <p>
 
1355
                          equivalent to:
 
1356
                        </p>
 
1357
                        <pre>Template::Plugin::URL-&gt;new(
 
1358
    $context, 
 
1359
    '/cgi-bin/foo'
 
1360
    { mode =&gt; 'submit', debug =&gt; 1 }
 
1361
);</pre>
 
1362
                        <p>
 
1363
                          The plugin may represent any data type; a simple variable, hash, list or
 
1364
                          code reference, but in the general case it will be an object reference.
 
1365
                          Methods can be called on the object (or the relevant members of the
 
1366
                          specific data type) in the usual way:
 
1367
                        </p>
 
1368
                        <pre>[% USE table(mydata, rows=3) %]
 
1369
 
 
1370
[% FOREACH row IN table.rows %]
 
1371
   &lt;tr&gt;    
 
1372
   [% FOREACH item IN row %]
 
1373
    &lt;td&gt;[% item %]&lt;/td&gt;
 
1374
   [% END %]
 
1375
   &lt;/tr&gt;
 
1376
[% END %]</pre>
 
1377
                        <p>
 
1378
                          An alternative name may be provided for the plugin by which it can be
 
1379
                          referenced:
 
1380
                        </p>
 
1381
                        <pre>[% USE scores = table(myscores, cols=5) %]
 
1382
 
 
1383
[% FOREACH row IN scores.rows %]
 
1384
   ...
 
1385
[% END %]</pre>
 
1386
                        <p>
 
1387
                          You can use this approach to create multiple plugin objects with
 
1388
                          different configurations. This example shows how the <a
 
1389
                          href="#section_format">Template::Plugin::Format</a> plugin is used to
 
1390
                          create sub-routines bound to variables for formatting text as per
 
1391
                          <code>printf()</code>.
 
1392
                        </p>
 
1393
                        <pre>[% USE bold = format('&lt;b&gt;%s&lt;/b&gt;') %]
 
1394
[% USE ital = format('&lt;i&gt;%s&lt;/i&gt;') %]
 
1395
[% bold('This is bold')   %]
 
1396
[% ital('This is italic') %]</pre>
 
1397
                        <p>
 
1398
                          Output:
 
1399
                        </p>
 
1400
                        <pre>&lt;b&gt;This is bold&lt;/b&gt;
 
1401
&lt;i&gt;This is italic&lt;/i&gt;</pre>
 
1402
                        <p>
 
1403
                          This next example shows how the <a
 
1404
                          href="#section_URL">Template::Plugin::URL</a> plugin can be used to build
 
1405
                          dynamic URLs from a base part and optional query parameters.
 
1406
                        </p>
 
1407
                        <pre>[% USE mycgi = URL('/cgi-bin/foo.pl', debug=1) %]
 
1408
&lt;a href="[% mycgi %]"&gt;...
 
1409
&lt;a href="[% mycgi(mode='submit') %]"...</pre>
 
1410
                        <p>
 
1411
                          Output:
 
1412
                        </p>
 
1413
                        <pre>&lt;a href="/cgi-bin/foo.pl?debug=1"&gt;...
 
1414
&lt;a href="/cgi-bin/foo.pl?mode=submit&amp;debug=1"&gt;...</pre>
 
1415
                        <p>
 
1416
                          The <a href="http://search.cpan.org/search?query=CGI&mode=all">Template::Plugin::CGI</a> plugin is
 
1417
                          an example of one which delegates to another Perl module. In this this
 
1418
                          case, to Lincoln Stein's <code>CGI</code> module. All of the methods
 
1419
                          provided by the <code>CGI</code> module are available via the plugin.
 
1420
                        </p>
 
1421
                        <pre>[% USE CGI;
 
1422
   CGI.start_form;
 
1423
   CGI.checkbox_group( name   = 'colours', 
 
1424
                       values = [ 'red' 'green' 'blue' ] );
 
1425
   CGI.popup_menu( name   = 'items', 
 
1426
                   values = [ 'foo' 'bar' 'baz' ] );
 
1427
   CGI.end_form 
 
1428
%]</pre>
 
1429
                        <p>
 
1430
                          See <a href="../manual/Plugins.html">Template::Manual::Plugins</a> for more information on the plugins
 
1431
                          distributed with the toolkit or available from CPAN.
 
1432
                        </p>
 
1433
                  </div>
 
1434
                </div>    <div class="subsection">
 
1435
                  <div class="head">
 
1436
                    <h2 id="section_MACRO" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">MACRO</h2>
 
1437
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
1438
                  </div>
 
1439
                  <div class="body">
 
1440
                    <p>
 
1441
                          The <code>MACRO</code> directive allows you to define a directive or
 
1442
                          directive block which is then evaluated each time the macro is called.
 
1443
                        </p>
 
1444
                        <pre>[% MACRO header INCLUDE header %]</pre>
 
1445
                        <p>
 
1446
                          Calling the macro as:
 
1447
                        </p>
 
1448
                        <pre>[% header %]</pre>
 
1449
                        <p>
 
1450
                          is then equivalent to:
 
1451
                        </p>
 
1452
                        <pre>[% INCLUDE header %]</pre>
 
1453
                        <p>
 
1454
                          Macros can be passed named parameters when called. These values remain
 
1455
                          local to the macro.
 
1456
                        </p>
 
1457
                        <pre>[% header(title='Hello World') %]  </pre>
 
1458
                        <p>
 
1459
                          equivalent to:
 
1460
                        </p>
 
1461
                        <pre>[% INCLUDE header title='Hello World' %]</pre>
 
1462
                        <p>
 
1463
                          A <code>MACRO</code> definition may include parameter names. Values
 
1464
                          passed to the macros are then mapped to these local variables. Other
 
1465
                          named parameters may follow these.
 
1466
                        </p>
 
1467
                        <pre>[% MACRO header(title) INCLUDE header %]
 
1468
[% header('Hello World') %]
 
1469
[% header('Hello World', bgcol='#123456') %]</pre>
 
1470
                        <p>
 
1471
                          equivalent to:
 
1472
                        </p>
 
1473
                        <pre>[% INCLUDE header title='Hello World' %]
 
1474
[% INCLUDE header title='Hello World' bgcol='#123456' %]</pre>
 
1475
                        <p>
 
1476
                          Here's another example, defining a macro for display numbers in
 
1477
                          comma-delimited groups of 3, using the chunk and join virtual method.
 
1478
                        </p>
 
1479
                        <pre>[% MACRO number(n) GET n.chunk(-3).join(',') %]
 
1480
[% number(1234567) %]    # 1,234,567</pre>
 
1481
                        <p>
 
1482
                          A <code>MACRO</code> may precede any directive and must conform to the
 
1483
                          structure of the directive.
 
1484
                        </p>
 
1485
                        <pre>[% MACRO header IF frames %]
 
1486
   [% INCLUDE frames/header %]
 
1487
[% ELSE %]
 
1488
   [% INCLUDE header %]
 
1489
[% END %]
 
1490
 
 
1491
[% header %]</pre>
 
1492
                        <p>
 
1493
                          A <code>MACRO</code> may also be defined as an anonymous
 
1494
                          <code>BLOCK</code>. The block will be evaluated each time the macro is
 
1495
                          called.
 
1496
                        </p>
 
1497
                        <pre>[% MACRO header BLOCK %]
 
1498
   ...content...
 
1499
[% END %]
 
1500
 
 
1501
[% header %]</pre>
 
1502
                        <p>
 
1503
                          If you've got the <code>EVAL_PERL</code> option set, then you can even
 
1504
                          define a <code>MACRO</code> as a <code>PERL</code> block (see below):
 
1505
                        </p>
 
1506
                        <pre>[% MACRO triple(n) PERL %]
 
1507
     my $n = $stash-&gt;get('n');
 
1508
     print $n * 3;
 
1509
[% END -%]</pre>
 
1510
                  </div>
 
1511
                </div>    <div class="subsection">
 
1512
                  <div class="head">
 
1513
                    <h2 id="section_PERL" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">PERL</h2>
 
1514
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
1515
                  </div>
 
1516
                  <div class="body">
 
1517
                    <p>
 
1518
                          (for the advanced reader)
 
1519
                        </p>
 
1520
                        <p>
 
1521
                          The <code>PERL</code> directive is used to mark the start of a block
 
1522
                          which contains Perl code for evaluation. The <code>EVAL_PERL</code>
 
1523
                          option must be enabled for Perl code to be evaluated or a
 
1524
                          <code>perl</code> exception will be thrown with the message
 
1525
                          '<code>EVAL_PERL not set</code>'.
 
1526
                        </p>
 
1527
                        <p>
 
1528
                          Perl code is evaluated in the <code>Template::Perl</code> package. The
 
1529
                          <code>$context</code> package variable contains a reference to the
 
1530
                          current <a href="../modules/Template/Context.html">Template::Context</a> object. This can be used to access the
 
1531
                          functionality of the Template Toolkit to process other templates, load
 
1532
                          plugins, filters, etc. See <a href="../modules/Template/Context.html">Template::Context</a> for further details.
 
1533
                        </p>
 
1534
                        <pre>[% PERL %]
 
1535
   print $context-&gt;include('myfile');
 
1536
[% END %]</pre>
 
1537
                        <p>
 
1538
                          The <a href="#section_$stash">$stash</a> variable contains a reference to
 
1539
                          the top-level stash object which manages template variables. Through
 
1540
                          this, variable values can be retrieved and updated. See <a href="../modules/Template/Stash.html">Template::Stash</a> for further
 
1541
                          details.
 
1542
                        </p>
 
1543
                        <pre>[% PERL %]
 
1544
   $stash-&gt;set(foo =&gt; 'bar');
 
1545
   print "foo value: ", $stash-&gt;get('foo');
 
1546
[% END %]</pre>
 
1547
                        <p>
 
1548
                          Output:
 
1549
                        </p>
 
1550
                        <pre>foo value: bar</pre>
 
1551
                        <p>
 
1552
                          Output is generated from the <code>PERL</code> block by calling
 
1553
                          <code>print()</code>. Note that the <code>Template::Perl::PERLOUT</code>
 
1554
                          handle is selected (tied to an output buffer) instead of
 
1555
                          <code>STDOUT</code>.
 
1556
                        </p>
 
1557
                        <pre>[% PERL %]
 
1558
   print "foo\n";                           # OK
 
1559
   print PERLOUT "bar\n";                   # OK, same as above
 
1560
   print Template::Perl::PERLOUT "baz\n";   # OK, same as above
 
1561
   print STDOUT "qux\n";                    # WRONG!
 
1562
[% END %]</pre>
 
1563
                        <p>
 
1564
                          The <code>PERL</code> block may contain other template directives. These
 
1565
                          are processed before the Perl code is evaluated.
 
1566
                        </p>
 
1567
                        <pre>[% name = 'Fred Smith' %]
 
1568
 
 
1569
[% PERL %]
 
1570
   print "[% name %]\n";
 
1571
[% END %]</pre>
 
1572
                        <p>
 
1573
                          Thus, the Perl code in the above example is evaluated as:
 
1574
                        </p>
 
1575
                        <pre>print "Fred Smith\n";</pre>
 
1576
                        <p>
 
1577
                          Exceptions may be thrown from within <code>PERL</code> blocks using
 
1578
                          <code>die()</code>. They will be correctly caught by enclosing
 
1579
                          <code>TRY</code> blocks.
 
1580
                        </p>
 
1581
                        <pre>[% TRY %]
 
1582
   [% PERL %]
 
1583
      die "nothing to live for\n";
 
1584
   [% END %]
 
1585
[% CATCH %]
 
1586
   error: [% error.info %]
 
1587
[% END %]</pre>
 
1588
                        <p>
 
1589
                          output: error: nothing to live for
 
1590
                        </p>
 
1591
                  </div>
 
1592
                </div>    <div class="subsection">
 
1593
                  <div class="head">
 
1594
                    <h2 id="section_RAWPERL" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">RAWPERL</h2>
 
1595
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
1596
                  </div>
 
1597
                  <div class="body">
 
1598
                    <p>
 
1599
                          (for the very advanced reader)
 
1600
                        </p>
 
1601
                        <p>
 
1602
                          The Template Toolkit parser reads a source template and generates the
 
1603
                          text of a Perl subroutine as output. It then uses <code>eval()</code> to
 
1604
                          evaluate it into a subroutine reference. This subroutine is then called
 
1605
                          to process the template, passing a reference to the current <a href="../modules/Template/Context.html">Template::Context</a> object through
 
1606
                          which the functionality of the Template Toolkit can be accessed. The
 
1607
                          subroutine reference can be cached, allowing the template to be processed
 
1608
                          repeatedly without requiring any further parsing.
 
1609
                        </p>
 
1610
                        <p>
 
1611
                          For example, a template such as:
 
1612
                        </p>
 
1613
                        <pre>[% PROCESS header %]
 
1614
The [% animal %] sat on the [% location %]
 
1615
[% PROCESS footer %]</pre>
 
1616
                        <p>
 
1617
                          is converted into the following Perl subroutine definition:
 
1618
                        </p>
 
1619
                        <pre>sub {
 
1620
    my $context = shift;
 
1621
    my $stash   = $context-&gt;stash;
 
1622
    my $output  = '';
 
1623
    my $error;
 
1624
 
 
1625
    eval { BLOCK: {
 
1626
        $output .=  $context-&gt;process('header');
 
1627
        $output .=  "The ";
 
1628
        $output .=  $stash-&gt;get('animal');
 
1629
        $output .=  " sat on the ";
 
1630
        $output .=  $stash-&gt;get('location');
 
1631
        $output .=  $context-&gt;process('footer');
 
1632
        $output .=  "\n";
 
1633
    } };
 
1634
    if ($@) {
 
1635
        $error = $context-&gt;catch($@, \$output);
 
1636
        die $error unless $error-&gt;type eq 'return';
 
1637
    }
 
1638
 
 
1639
    return $output;
 
1640
}</pre>
 
1641
                        <p>
 
1642
                          To examine the Perl code generated, such as in the above example, set the
 
1643
                          <code>$Template::Parser::DEBUG</code> package variable to any true value.
 
1644
                          You can also set the <code>$Template::Directive::PRETTY</code> variable
 
1645
                          true to have the code formatted in a readable manner for human
 
1646
                          consumption. The source code for each generated template subroutine will
 
1647
                          be printed to <code>STDERR</code> on compilation (i.e. the first time a
 
1648
                          template is used).
 
1649
                        </p>
 
1650
                        <pre>$Template::Parser::DEBUG = 1;
 
1651
$Template::Directive::PRETTY = 1;
 
1652
 
 
1653
$template-&gt;process($file, $vars)
 
1654
    || die $template-&gt;error(), "\n";</pre>
 
1655
                        <p>
 
1656
                          The <code>PERL</code> ... <code>END</code> construct allows Perl code to
 
1657
                          be embedded into a template when the <code>EVAL_PERL</code> option is
 
1658
                          set. It is evaluated at "runtime" using <code>eval()</code> each time the
 
1659
                          template subroutine is called. This is inherently flexible, but not as
 
1660
                          efficient as it could be, especially in a persistent server environment
 
1661
                          where a template may be processed many times.
 
1662
                        </p>
 
1663
                        <p>
 
1664
                          The <code>RAWPERL</code> directive allows you to write Perl code that is
 
1665
                          integrated directly into the generated Perl subroutine text. It is
 
1666
                          evaluated once at compile time and is stored in cached form as part of
 
1667
                          the compiled template subroutine. This makes <code>RAWPERL</code> blocks
 
1668
                          more efficient than <code>PERL</code> blocks.
 
1669
                        </p>
 
1670
                        <p>
 
1671
                          The downside is that you must code much closer to the metal. For example,
 
1672
                          in a <code>PERL</code> block you can call <a
 
1673
                          href="#method_print">print()</a> to generate some output.
 
1674
                          <code>RAWPERL</code> blocks don't afford such luxury. The code is
 
1675
                          inserted directly into the generated subroutine text and should conform
 
1676
                          to the convention of appending to the <code>$output</code> variable.
 
1677
                        </p>
 
1678
                        <pre>[% PROCESS  header %]
 
1679
 
 
1680
[% RAWPERL %]
 
1681
   $output .= "Some output\n";
 
1682
   ...
 
1683
   $output .= "Some more output\n";
 
1684
[% END %]</pre>
 
1685
                        <p>
 
1686
                          The critical section of the generated subroutine for this example would
 
1687
                          then look something like:
 
1688
                        </p>
 
1689
                        <pre>...
 
1690
eval { BLOCK: {
 
1691
    $output .=  $context-&gt;process('header');
 
1692
    $output .=  "\n";
 
1693
    $output .= "Some output\n";
 
1694
    ...
 
1695
    $output .= "Some more output\n";
 
1696
    $output .=  "\n";
 
1697
} };
 
1698
...</pre>
 
1699
                        <p>
 
1700
                          As with <code>PERL</code> blocks, the <a
 
1701
                          href="#section_$context">Template::Context</a> and <a
 
1702
                          href="#section_$stash">Template::Stash</a> references are pre-defined and
 
1703
                          available for use within <code>RAWPERL</code> code.
 
1704
                        </p>
 
1705
                  </div>
 
1706
                </div>
 
1707
              </div>
 
1708
            </div>
 
1709
            <div class="section">
 
1710
              <div class="head">
 
1711
                <h1 id="Exception_Handling_and_Flow_Control" onclick="switch_section(this)" title="Click title to show/hide section content.">Exception Handling and Flow Control</h1>
 
1712
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
1713
              </div>
 
1714
              <div class="body">
 
1715
                <div class="subsection">
 
1716
                  <div class="head">
 
1717
                    <h2 id="section_TRY_THROW_CATCH_FINAL" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">TRY / THROW / CATCH / FINAL</h2>
 
1718
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
1719
                  </div>
 
1720
                  <div class="body">
 
1721
                    <p>
 
1722
                          (more advanced material)
 
1723
                        </p>
 
1724
                        <p>
 
1725
                          The Template Toolkit supports fully functional, nested exception
 
1726
                          handling. The <code>TRY</code> directive introduces an exception handling
 
1727
                          scope which continues until the matching <code>END</code> directive. Any
 
1728
                          errors that occur within that block will be caught and can be handled by
 
1729
                          one of the <code>CATCH</code> blocks defined.
 
1730
                        </p>
 
1731
                        <pre>[% TRY %]
 
1732
   ...blah...blah...
 
1733
   [% CALL somecode %]
 
1734
   ...etc...
 
1735
   [% INCLUDE someblock %]
 
1736
   ...and so on...
 
1737
[% CATCH %]
 
1738
   An error occurred!
 
1739
[% END %]</pre>
 
1740
                        <p>
 
1741
                          Errors are raised as exceptions (objects of the <a href="../modules/Template/Exception.html">Template::Exception</a> class)
 
1742
                          which contain two fields: <code>type</code> and <code>info</code>. The
 
1743
                          exception <code>type</code> is used to indicate the kind of error that
 
1744
                          occurred. It is a simple text string which can contain letters, numbers,
 
1745
                          '<code>_</code>' or '<code>.</code>'. The <code>info</code> field
 
1746
                          contains an error message indicating what actually went wrong. Within a
 
1747
                          catch block, the exception object is aliased to the <code>error</code>
 
1748
                          variable. You can access the <code>type</code> and <code>info</code>
 
1749
                          fields directly.
 
1750
                        </p>
 
1751
                        <pre>[% mydsn = 'dbi:MySQL:foobar' %]
 
1752
...
 
1753
 
 
1754
[% TRY %]
 
1755
   [% USE DBI(mydsn) %]
 
1756
[% CATCH %]
 
1757
   ERROR! Type: [% error.type %]
 
1758
          Info: [% error.info %]
 
1759
[% END %]</pre>
 
1760
                        <p>
 
1761
                          output (assuming a non-existant database called '<code>foobar</code>'):
 
1762
                        </p>
 
1763
                        <pre>ERROR!  Type: DBI
 
1764
        Info: Unknown database "foobar"</pre>
 
1765
                        <p>
 
1766
                          The <code>error</code> variable can also be specified by itself and will
 
1767
                          return a string of the form "<code>$type error - $info</code>".
 
1768
                        </p>
 
1769
                        <pre>...
 
1770
[% CATCH %]
 
1771
ERROR: [% error %]
 
1772
[% END %]</pre>
 
1773
                        <p>
 
1774
                          Output:
 
1775
                        </p>
 
1776
                        <pre>ERROR: DBI error - Unknown database "foobar"</pre>
 
1777
                        <p>
 
1778
                          Each <code>CATCH</code> block may be specified with a particular
 
1779
                          exception type denoting the kind of error that it should catch. Multiple
 
1780
                          <code>CATCH</code> blocks can be provided to handle different types of
 
1781
                          exception that may be thrown in the <code>TRY</code> block. A
 
1782
                          <code>CATCH</code> block specified without any type, as in the previous
 
1783
                          example, is a default handler which will catch any otherwise uncaught
 
1784
                          exceptions. This can also be specified as <code>[% CATCH DEFAULT
 
1785
                          %]</code>.
 
1786
                        </p>
 
1787
                        <pre>[% TRY %]
 
1788
   [% INCLUDE myfile %]
 
1789
   [% USE DBI(mydsn) %]
 
1790
   [% CALL somecode %]
 
1791
[% CATCH file %]
 
1792
   File Error! [% error.info %]
 
1793
[% CATCH DBI %]
 
1794
   [% INCLUDE database/error.html %]
 
1795
[% CATCH %]
 
1796
   [% error %]
 
1797
[% END %]</pre>
 
1798
                        <p>
 
1799
                          Remember that you can specify multiple directives within a single tag,
 
1800
                          each delimited by '<code>;</code>'. So the above example can be written
 
1801
                          more concisely as:
 
1802
                        </p>
 
1803
                        <pre>[% TRY;
 
1804
       INCLUDE myfile;
 
1805
       USE DBI(mydsn);
 
1806
       CALL somecode;
 
1807
   CATCH file;
 
1808
       "File Error! $error.info";
 
1809
   CATCH DBI;
 
1810
       INCLUDE database/error.html;
 
1811
   CATCH;
 
1812
       error;
 
1813
   END 
 
1814
%]</pre>
 
1815
                        <p>
 
1816
                          The <code>DBI</code> plugin throws exceptions of the <code>DBI</code>
 
1817
                          type (in case that wasn't already obvious). The other specific exception
 
1818
                          caught here is of the <code>file</code> type.
 
1819
                        </p>
 
1820
                        <p>
 
1821
                          A <code>file</code> exception is automatically thrown by the Template
 
1822
                          Toolkit when it can't find a file, or fails to load, parse or process a
 
1823
                          file that has been requested by an <code>INCLUDE</code>,
 
1824
                          <code>PROCESS</code>, <code>INSERT</code> or <code>WRAPPER</code>
 
1825
                          directive. If <code>myfile</code> can't be found in the example above,
 
1826
                          the <code>[% INCLUDE myfile %]</code> directive will raise a
 
1827
                          <code>file</code> exception which is then caught by the <code>[% CATCH
 
1828
                          file %]</code> block. The output generated would be:
 
1829
                        </p>
 
1830
                        <pre>File Error! myfile: not found</pre>
 
1831
                        <p>
 
1832
                          Note that the <code>DEFAULT</code> option (disabled by default) allows
 
1833
                          you to specify a default file to be used any time a template file can't
 
1834
                          be found. This will prevent file exceptions from ever being raised when a
 
1835
                          non-existant file is requested (unless, of course, the
 
1836
                          <code>DEFAULT</code> file your specify doesn't exist). Errors encountered
 
1837
                          once the file has been found (i.e. read error, parse error) will be
 
1838
                          raised as file exceptions as per usual.
 
1839
                        </p>
 
1840
                        <p>
 
1841
                          Uncaught exceptions (i.e. if the <code>TRY</code> block doesn't have a
 
1842
                          type specific or default <code>CATCH</code> handler) may be caught by
 
1843
                          enclosing <code>TRY</code> blocks which can be nested indefinitely across
 
1844
                          multiple templates. If the error isn't caught at any level then
 
1845
                          processing will stop and the Template <a
 
1846
                          href="#method_process">Template#process()</a> method will return a false
 
1847
                          value to the caller. The relevant <a href="../modules/Template/Exception.html">Template::Exception</a> object can
 
1848
                          be retrieved by calling the <a href="#method_error">Template#error()</a>
 
1849
                          method.
 
1850
                        </p>
 
1851
                        <pre>[% TRY %]
 
1852
   ...
 
1853
   [% TRY %]
 
1854
      [% INCLUDE $user.header %]
 
1855
   [% CATCH file %]
 
1856
      [% INCLUDE header %]
 
1857
   [% END %]
 
1858
   ...
 
1859
[% CATCH DBI %]
 
1860
   [% INCLUDE database/error.html %]
 
1861
[% END %]</pre>
 
1862
                        <p>
 
1863
                          In this example, the inner <code>TRY</code> block is used to ensure that
 
1864
                          the first <code>INCLUDE</code> directive works as expected. We're using a
 
1865
                          variable to provide the name of the template we want to include,
 
1866
                          <code>user.header</code>, and it's possible this contains the name of a
 
1867
                          non-existant template, or perhaps one containing invalid template
 
1868
                          directives. If the <code>INCLUDE</code> fails with a <code>file</code>
 
1869
                          error then we <code>CATCH</code> it in the inner block and
 
1870
                          <code>INCLUDE</code> the default <code>header</code> file instead. Any
 
1871
                          <code>DBI</code> errors that occur within the scope of the outer
 
1872
                          <code>TRY</code> block will be caught in the relevant <code>CATCH</code>
 
1873
                          block, causing the <code>database/error.html</code> template to be
 
1874
                          processed. Note that included templates inherit all currently defined
 
1875
                          template variable so these error files can quite happily access the
 
1876
                          &lt;error&gt; variable to retrieve information about the currently caught
 
1877
                          exception. For example, the <code>database/error.html</code> template
 
1878
                          might look like this:
 
1879
                        </p>
 
1880
                        <pre>&lt;h2&gt;Database Error&lt;/h2&gt;
 
1881
A database error has occurred: [% error.info %]</pre>
 
1882
                        <p>
 
1883
                          You can also specify a <code>FINAL</code> block. This is always processed
 
1884
                          regardless of the outcome of the <code>TRY</code> and/or
 
1885
                          <code>CATCH</code> blocks. If an exception is uncaught then the
 
1886
                          <code>FINAL</code> block is processed before jumping to the enclosing
 
1887
                          block or returning to the caller.
 
1888
                        </p>
 
1889
                        <pre>[% TRY %]
 
1890
   ...
 
1891
[% CATCH this %] 
 
1892
   ...
 
1893
[% CATCH that %] 
 
1894
   ...
 
1895
[% FINAL %]
 
1896
   All done!
 
1897
[% END %]</pre>
 
1898
                        <p>
 
1899
                          The output from the <code>TRY</code> block is left intact up to the point
 
1900
                          where an exception occurs. For example, this template:
 
1901
                        </p>
 
1902
                        <pre>[% TRY %]
 
1903
   This gets printed 
 
1904
   [% THROW food 'carrots' %]
 
1905
   This doesn't
 
1906
[% CATCH food %]
 
1907
   culinary delights: [% error.info %]
 
1908
[% END %]    </pre>
 
1909
                        <p>
 
1910
                          generates the following output:
 
1911
                        </p>
 
1912
                        <pre>This gets printed
 
1913
culinary delights: carrots</pre>
 
1914
                        <p>
 
1915
                          The <code>CLEAR</code> directive can be used in a <code>CATCH</code> or
 
1916
                          <code>FINAL</code> block to clear any output created in the
 
1917
                          <code>TRY</code> block.
 
1918
                        </p>
 
1919
                        <pre>[% TRY %]
 
1920
   This gets printed 
 
1921
   [% THROW food 'carrots' %]
 
1922
   This doesn't
 
1923
[% CATCH food %]
 
1924
   [% CLEAR %]
 
1925
   culinary delights: [% error.info %]
 
1926
[% END %]    </pre>
 
1927
                        <p>
 
1928
                          Output:
 
1929
                        </p>
 
1930
                        <pre>culinary delights: carrots</pre>
 
1931
                        <p>
 
1932
                          Exception types are hierarchical, with each level being separated by the
 
1933
                          familiar dot operator. A <code>DBI.connect</code> exception is a more
 
1934
                          specific kind of <code>DBI</code> error. Similarly, an
 
1935
                          <code>example.error.barf</code> is a more specific kind of
 
1936
                          <code>example.error</code> type which itself is also a
 
1937
                          <code>example</code> error.
 
1938
                        </p>
 
1939
                        <p>
 
1940
                          A <code>CATCH</code> handler that specifies a general exception type
 
1941
                          (such as <code>DBI</code> or <code>example.error</code>) will also catch
 
1942
                          more specific types that have the same prefix as long as a more specific
 
1943
                          handler isn't defined. Note that the order in which <code>CATCH</code>
 
1944
                          handlers are defined is irrelevant; a more specific handler will always
 
1945
                          catch an exception in preference to a more generic or default one.
 
1946
                        </p>
 
1947
                        <pre>[% TRY %]
 
1948
   ...
 
1949
[% CATCH DBI ;
 
1950
     INCLUDE database/error.html ;
 
1951
   CATCH DBI.connect ;
 
1952
     INCLUDE database/connect.html ;
 
1953
   CATCH ; 
 
1954
     INCLUDE error.html ;
 
1955
   END
 
1956
%]</pre>
 
1957
                        <p>
 
1958
                          In this example, a <code>DBI.connect</code> error has it's own handler, a
 
1959
                          more general <code>DBI</code> block is used for all other
 
1960
                          <code>DBI</code> or <code>DBI.*</code> errors and a default handler
 
1961
                          catches everything else.
 
1962
                        </p>
 
1963
                        <p>
 
1964
                          Exceptions can be raised in a template using the <code>THROW</code>
 
1965
                          directive. The first parameter is the exception type which doesn't need
 
1966
                          to be quoted (but can be, it's the same as <code>INCLUDE</code>) followed
 
1967
                          by the relevant error message which can be any regular value such as a
 
1968
                          quoted string, variable, etc.
 
1969
                        </p>
 
1970
                        <pre>[% THROW food "Missing ingredients: $recipe.error" %]
 
1971
[% THROW user.login 'no user id: please login' %]
 
1972
[% THROW $myerror.type "My Error: $myerror.info" %]</pre>
 
1973
                        <p>
 
1974
                          It's also possible to specify additional positional or named parameters
 
1975
                          to the <code>THROW</code> directive if you want to pass more than just a
 
1976
                          simple message back as the error info field.
 
1977
                        </p>
 
1978
                        <pre>[% THROW food 'eggs' 'flour' msg='Missing Ingredients' %]</pre>
 
1979
                        <p>
 
1980
                          In this case, the error <code>info</code> field will be a hash array
 
1981
                          containing the named arguments and an <code>args</code> item which
 
1982
                          contains a list of the positional arguments.
 
1983
                        </p>
 
1984
                        <pre>type =&gt; 'food',
 
1985
info =&gt; {
 
1986
    msg  =&gt; 'Missing Ingredients',
 
1987
    args =&gt; ['eggs', 'flour'],
 
1988
}</pre>
 
1989
                        <p>
 
1990
                          In addition to specifying individual positional arguments as <code>[%
 
1991
                          error.info.args.n %]</code>, the <code>info</code> hash contains keys
 
1992
                          directly pointing to the positional arguments, as a convenient shortcut.
 
1993
                        </p>
 
1994
                        <pre>[% error.info.0 %]   # same as [% error.info.args.0 %]</pre>
 
1995
                        <p>
 
1996
                          Exceptions can also be thrown from Perl code which you've bound to
 
1997
                          template variables, or defined as a plugin or other extension. To raise
 
1998
                          an exception, call <code>die()</code> passing a reference to a <a
 
1999
                          href="../modules/Template/Exception.html">Template::Exception</a>
 
2000
                          object as the argument. This will then be caught by any enclosing
 
2001
                          <code>TRY</code> blocks from where the code was called.
 
2002
                        </p>
 
2003
                        <pre>use Template::Exception;
 
2004
...
 
2005
my $vars = {
 
2006
    foo =&gt; sub {
 
2007
        # ... do something ...
 
2008
        die Template::Exception-&gt;new('myerr.naughty',
 
2009
                                     'Bad, bad error');
 
2010
    },
 
2011
};</pre>
 
2012
                        <p>
 
2013
                          Template:
 
2014
                        </p>
 
2015
                        <pre>[% TRY %]
 
2016
   [% foo %]
 
2017
[% CATCH myerr ;
 
2018
     "Error: $error" ;
 
2019
   END
 
2020
%]</pre>
 
2021
                        <p>
 
2022
                          Output:
 
2023
                        </p>
 
2024
                        <pre>Error: myerr.naughty error - Bad, bad error</pre>
 
2025
                        <p>
 
2026
                          The <code>info</code> field can also be a reference to another object or
 
2027
                          data structure, if required.
 
2028
                        </p>
 
2029
                        <pre>die Template::Exception-&gt;new('myerror', { 
 
2030
    module =&gt; 'foo.pl', 
 
2031
    errors =&gt; [ 'bad permissions', 'naughty boy' ],
 
2032
});</pre>
 
2033
                        <p>
 
2034
                          Later, in a template:
 
2035
                        </p>
 
2036
                        <pre>[% TRY %]
 
2037
   ...
 
2038
[% CATCH myerror %]
 
2039
   [% error.info.errors.size or 'no';
 
2040
      error.info.errors.size == 1 ? ' error' : ' errors' %]
 
2041
   in [% error.info.module %]: 
 
2042
      [% error.info.errors.join(', ') %].
 
2043
[% END %]</pre>
 
2044
                        <p>
 
2045
                          Generating the output:
 
2046
                        </p>
 
2047
                        <pre>2 errors in foo.pl:
 
2048
   bad permissions, naughty boy.</pre>
 
2049
                        <p>
 
2050
                          You can also call <code>die()</code> with a single string, as is common
 
2051
                          in much existing Perl code. This will automatically be converted to an
 
2052
                          exception of the '<code>undef</code>' type (that's the literal string
 
2053
                          '<code>undef</code>', not the undefined value). If the string isn't
 
2054
                          terminated with a newline then Perl will append the familiar <code>" at
 
2055
                          $file line $line"</code> message.
 
2056
                        </p>
 
2057
                        <pre>sub foo {
 
2058
    # ... do something ...
 
2059
    die "I'm sorry, Dave, I can't do that\n";
 
2060
}</pre>
 
2061
                        <p>
 
2062
                          If you're writing a plugin, or some extension code that has the current
 
2063
                          <a href="../modules/Template/Context.html">Template::Context</a> in
 
2064
                          scope (you can safely skip this section if this means nothing to you)
 
2065
                          then you can also raise an exception by calling the context <a
 
2066
                          href="#method_throw">Template::Context#throw()</a> method. You can pass
 
2067
                          it an <a href="../modules/Template/Exception.html">Template::Exception</a> object reference, a pair of <code>($type,
 
2068
                          $info)</code> parameters or just an <code>$info</code> string to create
 
2069
                          an exception of '<code>undef</code>' type.
 
2070
                        </p>
 
2071
                        <pre>$context-&gt;throw($e);            # exception object
 
2072
$context-&gt;throw('Denied');      # 'undef' type
 
2073
$context-&gt;throw('user.passwd', 'Bad Password');</pre>
 
2074
                  </div>
 
2075
                </div>    <div class="subsection">
 
2076
                  <div class="head">
 
2077
                    <h2 id="section_NEXT" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">NEXT</h2>
 
2078
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
2079
                  </div>
 
2080
                  <div class="body">
 
2081
                    <p>
 
2082
                          The <code>NEXT</code> directive can be used to start the next iteration
 
2083
                          of a <code>FOREACH</code> or <code>WHILE</code> loop.
 
2084
                        </p>
 
2085
                        <pre>[% FOREACH user IN users %]
 
2086
   [% NEXT IF user.isguest %]
 
2087
   Name: [% user.name %]    Email: [% user.email %]
 
2088
[% END %]</pre>
 
2089
                  </div>
 
2090
                </div>    <div class="subsection">
 
2091
                  <div class="head">
 
2092
                    <h2 id="section_LAST" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">LAST</h2>
 
2093
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
2094
                  </div>
 
2095
                  <div class="body">
 
2096
                    <p>
 
2097
                          The <code>LAST</code> directive can be used to prematurely exit a
 
2098
                          <code>FOREACH</code> or <code>WHILE</code> loop.
 
2099
                        </p>
 
2100
                        <pre>[% FOREACH user IN users %]
 
2101
   Name: [% user.name %]    Email: [% user.email %]
 
2102
   [% LAST IF some.condition %]
 
2103
[% END %]</pre>
 
2104
                        <p>
 
2105
                          <code>BREAK</code> can also be used as an alias for <code>LAST</code>.
 
2106
                        </p>
 
2107
                  </div>
 
2108
                </div>    <div class="subsection">
 
2109
                  <div class="head">
 
2110
                    <h2 id="section_RETURN" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">RETURN</h2>
 
2111
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
2112
                  </div>
 
2113
                  <div class="body">
 
2114
                    <p>
 
2115
                          The <code>RETURN</code> directive can be used to stop processing the
 
2116
                          current template and return to the template from which it was called,
 
2117
                          resuming processing at the point immediately after the
 
2118
                          <code>INCLUDE</code>, <code>PROCESS</code> or <code>WRAPPER</code>
 
2119
                          directive. If there is no enclosing template then the Template <a
 
2120
                          href="#method_process">Template#process()</a> method will return to the
 
2121
                          calling code with a true value.
 
2122
                        </p>
 
2123
                        <pre>Before
 
2124
[% INCLUDE half_wit %]
 
2125
After
 
2126
 
 
2127
[% BLOCK half_wit %]
 
2128
This is just half...
 
2129
[% RETURN %]
 
2130
...a complete block
 
2131
[% END %]</pre>
 
2132
                        <p>
 
2133
                          Output:
 
2134
                        </p>
 
2135
                        <pre>Before
 
2136
This is just half...
 
2137
After</pre>
 
2138
                  </div>
 
2139
                </div>    <div class="subsection">
 
2140
                  <div class="head">
 
2141
                    <h2 id="section_STOP" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">STOP</h2>
 
2142
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
2143
                  </div>
 
2144
                  <div class="body">
 
2145
                    <p>
 
2146
                          The <code>STOP</code> directive can be used to indicate that the
 
2147
                          processor should stop gracefully without processing any more of the
 
2148
                          template document. This is a planned stop and the Template <a
 
2149
                          href="#method_process">Template#process()</a> method will return a
 
2150
                          <b>true</b> value to the caller. This indicates that the template was
 
2151
                          processed successfully according to the directives within it.
 
2152
                        </p>
 
2153
                        <pre>[% IF something.terrible.happened %]
 
2154
   [% INCLUDE fatal/error.html %]
 
2155
   [% STOP %]
 
2156
[% END %]
 
2157
 
 
2158
[% TRY %]
 
2159
   [% USE DBI(mydsn) %]
 
2160
   ...
 
2161
[% CATCH DBI.connect %]
 
2162
   &lt;h1&gt;Cannot connect to the database: [% error.info %]&lt;/h1&gt;
 
2163
   &lt;p&gt;
 
2164
     We apologise for the inconvenience.
 
2165
   &lt;/p&gt;
 
2166
   [% INCLUDE footer %]
 
2167
   [% STOP %]
 
2168
[% END %]</pre>
 
2169
                  </div>
 
2170
                </div>    <div class="subsection">
 
2171
                  <div class="head">
 
2172
                    <h2 id="section_CLEAR" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">CLEAR</h2>
 
2173
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
2174
                  </div>
 
2175
                  <div class="body">
 
2176
                    <p>
 
2177
                          The <code>CLEAR</code> directive can be used to clear the output buffer
 
2178
                          for the current enclosing block. It is most commonly used to clear the
 
2179
                          output generated from a <code>TRY</code> block up to the point where the
 
2180
                          error occurred.
 
2181
                        </p>
 
2182
                        <pre>[% TRY %]
 
2183
   blah blah blah            # this is normally left intact
 
2184
   [% THROW some 'error' %]  # up to the point of error
 
2185
   ...
 
2186
[% CATCH %]
 
2187
   [% CLEAR %]               # clear the TRY output
 
2188
   [% error %]               # print error string
 
2189
[% END %]</pre>
 
2190
                  </div>
 
2191
                </div>
 
2192
              </div>
 
2193
            </div>
 
2194
            <div class="section">
 
2195
              <div class="head">
 
2196
                <h1 id="Miscellaneous" onclick="switch_section(this)" title="Click title to show/hide section content.">Miscellaneous</h1>
 
2197
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
2198
              </div>
 
2199
              <div class="body">
 
2200
                <div class="subsection">
 
2201
                  <div class="head">
 
2202
                    <h2 id="section_META" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">META</h2>
 
2203
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
2204
                  </div>
 
2205
                  <div class="body">
 
2206
                    <p>
 
2207
                          The <code>META</code> directive allows simple metadata items to be
 
2208
                          defined within a template. These are evaluated when the template is
 
2209
                          parsed and as such may only contain simple values (e.g. it's not possible
 
2210
                          to interpolate other variables values into <code>META</code> variables).
 
2211
                        </p>
 
2212
                        <pre>[% META
 
2213
   title   = 'The Cat in the Hat'
 
2214
   author  = 'Dr. Seuss'
 
2215
   version = 1.23 
 
2216
%]</pre>
 
2217
                        <p>
 
2218
                          The <code>template</code> variable contains a reference to the main
 
2219
                          template being processed. These metadata items may be retrieved as
 
2220
                          attributes of the template.
 
2221
                        </p>
 
2222
                        <pre>&lt;h1&gt;[% template.title %]&lt;/h1&gt;
 
2223
&lt;h2&gt;[% template.author %]&lt;/h2&gt;</pre>
 
2224
                        <p>
 
2225
                          The <code>name</code> and <code>modtime</code> metadata items are
 
2226
                          automatically defined for each template to contain its name and
 
2227
                          modification time in seconds since the epoch.
 
2228
                        </p>
 
2229
                        <pre>[% USE date %]              # use Date plugin to format time
 
2230
...
 
2231
[% template.name %] last modified
 
2232
at [% date.format(template.modtime) %]</pre>
 
2233
                        <p>
 
2234
                          The <code>PRE_PROCESS</code> and <code>POST_PROCESS</code> options allow
 
2235
                          common headers and footers to be added to all templates. The
 
2236
                          <code>template</code> reference is correctly defined when these templates
 
2237
                          are processed, allowing headers and footers to reference metadata items
 
2238
                          from the main template.
 
2239
                        </p>
 
2240
                        <pre>$template = Template-&gt;new({
 
2241
    PRE_PROCESS  =&gt; 'header',
 
2242
    POST_PROCESS =&gt; 'footer',
 
2243
});
 
2244
 
 
2245
$template-&gt;process('cat_in_hat');</pre>
 
2246
                        <p>
 
2247
                          header:
 
2248
                        </p>
 
2249
                        <pre>&lt;html&gt;
 
2250
  &lt;head&gt;
 
2251
    &lt;title&gt;[% template.title %]&lt;/title&gt;
 
2252
  &lt;/head&gt;
 
2253
  &lt;body&gt;</pre>
 
2254
                        <p>
 
2255
                          cat_in_hat:
 
2256
                        </p>
 
2257
                        <pre>[% META
 
2258
     title   = 'The Cat in the Hat'
 
2259
     author  = 'Dr. Seuss'
 
2260
     version = 1.23 
 
2261
     year    = 2000
 
2262
%]
 
2263
 
 
2264
    The cat in the hat sat on the mat.</pre>
 
2265
                        <p>
 
2266
                          footer:
 
2267
                        </p>
 
2268
                        <pre>    &lt;hr&gt;
 
2269
    &amp;copy; [% template.year %] [% template.author %]
 
2270
  &lt;/body&gt;
 
2271
&lt;/html&gt;</pre>
 
2272
                        <p>
 
2273
                          The output generated from the above example is:
 
2274
                        </p>
 
2275
                        <pre>&lt;html&gt;
 
2276
  &lt;head&gt;
 
2277
    &lt;title&gt;The Cat in the Hat&lt;/title&gt;
 
2278
  &lt;/head&gt;
 
2279
  &lt;body&gt;
 
2280
    The cat in the hat sat on the mat.
 
2281
    &lt;hr&gt;
 
2282
    &amp;copy; 2000 Dr. Seuss
 
2283
  &lt;/body&gt;
 
2284
&lt;/html&gt;</pre>
 
2285
                  </div>
 
2286
                </div>    <div class="subsection">
 
2287
                  <div class="head">
 
2288
                    <h2 id="section_TAGS" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">TAGS</h2>
 
2289
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
2290
                  </div>
 
2291
                  <div class="body">
 
2292
                    <p>
 
2293
                          The <code>TAGS</code> directive can be used to set the
 
2294
                          <code>START_TAG</code> and <code>END_TAG</code> values on a per-template
 
2295
                          file basis.
 
2296
                        </p>
 
2297
                        <pre>[% TAGS &lt;+ +&gt; %]
 
2298
 
 
2299
&lt;+ INCLUDE header +&gt;</pre>
 
2300
                        <p>
 
2301
                          The TAGS directive may also be used to set a named <code>TAG_STYLE</code>
 
2302
                        </p>
 
2303
                        <pre>[% TAGS html %]
 
2304
&lt;!-- INCLUDE header --&gt;</pre>
 
2305
                        <p>
 
2306
                          See the <a href="#section_TAGS">Template::Manual::Config#TAGS</a> and <a
 
2307
                          href="#section_TAG_STYLE">Template::Manual::Config#TAG_STYLE</a>
 
2308
                          configuration options for further details.
 
2309
                        </p>
 
2310
                  </div>
 
2311
                </div>    <div class="subsection">
 
2312
                  <div class="head">
 
2313
                    <h2 id="section_DEBUG" onclick="switch_subsection(this)" title="Click title to show/hide sub-section content.">DEBUG</h2>
 
2314
                    <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
2315
                  </div>
 
2316
                  <div class="body">
 
2317
                    <p>
 
2318
                          The <code>DEBUG</code> directive can be used to enable or disable
 
2319
                          directive debug messages within a template. The <code>DEBUG</code>
 
2320
                          configuration option must be set to include <code>DEBUG_DIRS</code> for
 
2321
                          the <code>DEBUG</code> directives to have any effect. If
 
2322
                          <code>DEBUG_DIRS</code> is not set then the parser will automatically
 
2323
                          ignore and remove any <code>DEBUG</code> directives.
 
2324
                        </p>
 
2325
                        <p>
 
2326
                          The <code>DEBUG</code> directive can be used with an <code>on</code> or
 
2327
                          <code>off</code> parameter to enable or disable directive debugging
 
2328
                          messages from that point forward. When enabled, the output of each
 
2329
                          directive in the generated output will be prefixed by a comment indicate
 
2330
                          the file, line and original directive text.
 
2331
                        </p>
 
2332
                        <pre>[% DEBUG on %]
 
2333
directive debugging is on (assuming DEBUG option is set true)
 
2334
[% DEBUG off %]
 
2335
directive debugging is off</pre>
 
2336
                        <p>
 
2337
                          The <code>format</code> parameter can be used to change the format of the
 
2338
                          debugging message.
 
2339
                        </p>
 
2340
                        <pre>[% DEBUG format '&lt;!-- $file line $line : [% $text %] --&gt;' %]</pre>
 
2341
                  </div>
 
2342
                </div>
 
2343
              </div>
 
2344
            </div>
 
2345
            
 
2346
            </div></div>
 
2347
          <br class="clear" />
 
2348
          <div class="pageinfo">
 
2349
            /manual/Directives.html last modified 10:57:39 31-May-2007
 
2350
          </div>
 
2351
        </div>
 
2352
        
 
2353
        <div id="footer">
 
2354
          <a href="http://opensource.org/" class="osi"></a>
 
2355
          <div class="controls">
 
2356
          <div class="pager">
 
2357
            <a href="../manual/Syntax.html" title="Template::Manual::Syntax" class="go back">Back<span class="about"><h4>Template::Manual::Syntax</h4></span></a>
 
2358
            <a href="../manual/index.html" title="Template::Manual" class="go up">Up<span class="about"><h4>Template::Manual</h4></span></a>
 
2359
            <a href="../manual/Variables.html" title="Template::Manual::Variables" class="go next">Next<span class="about"><h4>Template::Manual::Variables</h4></span></a>
 
2360
          </div>
 
2361
          </div>
 
2362
          <div class="copyright">
 
2363
            Copyright &copy; 1996-2007 <a href="http://wardley.org/">Andy Wardley</a>.  All Rights Reserved.
 
2364
          </div>
 
2365
          <div class="licence">
 
2366
            The <a href="http://template-toolkit.org/">Template Toolkit</a> is <a href="http://opensource.org/">Open Source</a> software.
 
2367
            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>
 
2368
            or the <a href="http://www.opensource.org/licenses/artistic-license.php">Perl Artistic Licence</a>.
 
2369
          </div>
 
2370
        </div>
 
2371
        <div id="palette">
 
2372
          <ul>
 
2373
            <li class="first"><a href="#" class="blue" onclick="set_style('Clear Blue')"></a></li>
 
2374
            <li><a href="#" class="orange" onclick="set_style('Clear Orange')"></a></li>
 
2375
            <li><a href="#" class="green" onclick="set_style('Clear Green')"></a></li>
 
2376
            <li><a href="#" class="purple" onclick="set_style('Clear Purple')"></a></li>
 
2377
            <li><a href="#" class="grey" onclick="set_style('Clear Grey')"></a></li>
 
2378
          </ul>
 
2379
        </div>
 
2380
    </div>  </body>
 
2381
</html>