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

« back to all changes in this revision

Viewing changes to html/old/manual/Internals.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::Internals</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/Internals.html">Internals</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/Plugins.html" title="Template::Manual::Plugins" class="go back">Back<span class="about"><h4>Template::Manual::Plugins</h4>Standard plugins</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/Views.html" title="Template::Manual::Views" class="go next">Next<span class="about"><h4>Template::Manual::Views</h4>Template Toolkit views (experimental)</span></a>
 
40
          </div>
 
41
          </div>
 
42
          <h1 class="headline">Template::Manual::Internals</h1>
 
43
          <h2 class="subhead">Template Toolkit internals</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">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" class="warm">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="#Outside_Looking_In">Outside Looking In</a></li>
 
79
                  <li class=""><a href="#Inside_Looking_Out">Inside Looking Out</a></li>
 
80
                  <li class=""><a href="#Hacking_on_the_Template_Toolkit">Hacking on the Template Toolkit</a></li>
 
81
              
 
82
              </ul>
 
83
            </div>
 
84
          </div>
 
85
          
 
86
                <div class="pod">
 
87
            <div class="section">
 
88
              <div class="head">
 
89
                <h1 id="Outside_Looking_In" onclick="switch_section(this)" title="Click title to show/hide section content.">Outside Looking In</h1>
 
90
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
91
              </div>
 
92
              <div class="body">
 
93
                <p>
 
94
                      The <a href="../modules/Template.html">Template</a> module is simply
 
95
                      a front end module which creates and uses a <a href="../modules/Template/Service.html">Template::Service</a> and pipes the
 
96
                      output wherever you want it to go (<code>STDOUT</code> by default, or
 
97
                      maybe a file, scalar, etc). The <code>Apache::Template</code> module
 
98
                      (available separately from CPAN) is another front end. That creates a
 
99
                      <code>Template::Service::Apache</code> object, calls on it as required
 
100
                      and sends the output back to the relevant <code>Apache::Request</code>
 
101
                      object.
 
102
                    </p>
 
103
                    <p>
 
104
                      These front-end modules are really only there to handle any specifics of
 
105
                      the environment in which they're being used. The
 
106
                      <code>Apache::Template</code> front end, for example, handles
 
107
                      <code>Apache::Request</code> specifics and configuration via the
 
108
                      <i>httpd.conf</i>. The regular <a href="../modules/Template.html">Template</a> front-end deals with <code>STDOUT</code>, variable
 
109
                      refs, etc. Otherwise it is <a href="../modules/Template/Service.html">Template::Service</a> (or subclass) which does all the work.
 
110
                    </p>
 
111
                    <p>
 
112
                      The <a href="../modules/Template/Service.html">Template::Service</a> module provides a high-quality template
 
113
                      delivery service, with bells, whistles, signed up service level agreement
 
114
                      and a 30-day no quibble money back guarantee. "Have a good time, all the
 
115
                      time", that's our motto.
 
116
                    </p>
 
117
                    <p>
 
118
                      Within the lower levels of the Template Toolkit, there are lots of messy
 
119
                      details that we generally don't want to have to worry about most of the
 
120
                      time. Things like templates not being found, or failing to parse
 
121
                      correctly, uncaught exceptions being thrown, missing plugin modules or
 
122
                      dependencies, and so on. <a href="../modules/Template/Service.html">Template::Service</a> hides that all away and makes everything look
 
123
                      simple to the outsider. It provides extra features, like
 
124
                      <code>PRE_PROCESS</code>, <code>PROCESS</code> and
 
125
                      <code>POST_PROCESS</code>, and also provides the error recovery mechanism
 
126
                      via <code>ERROR</code>. You ask it to process a template and it takes
 
127
                      care of everything for you. The <code>Template::Service::Apache</code>
 
128
                      module goes a little bit further, adding some extra headers to the <a
 
129
                      href="http://search.cpan.org/search?query=Apache::Request&mode=all">Apache::Request</a>, setting a
 
130
                      few extra template variables, and so on.
 
131
                    </p>
 
132
                    <p>
 
133
                      For the most part, the job of a service is really just one of scheduling
 
134
                      and dispatching. It receives a request in the form of a call to its <a
 
135
                      href="#method_process">Template::Service#process()</a> method and
 
136
                      schedules the named template specified as an argument, and possibly
 
137
                      several other templates (<code>PRE_PROCESS</code>, etc) to be processed
 
138
                      in order. It doesn't actually process the templates itself, but instead
 
139
                      makes a <a href="#method_process">Template::Context#process()</a> call
 
140
                      against a <a href="../modules/Template/Context.html">Template::Context</a> object.
 
141
                    </p>
 
142
                    <p>
 
143
                      <a href="../modules/Template/Context.html">Template::Context</a> is
 
144
                      the runtime engine for the Template Toolkit - the module that hangs
 
145
                      everything together in the lower levels of the Template Toolkit and that
 
146
                      one that does most of the real work, albeit by crafty delegation to
 
147
                      various other friendly helper modules.
 
148
                    </p>
 
149
                    <p>
 
150
                      Given a template name (or perhaps a reference to a scalar or file handle)
 
151
                      the context process() method must load and compile, or fetch a cached
 
152
                      copy of a previously compiled template, corresponding to that name. It
 
153
                      does this by calling on a list of one or more <a href="../modules/Template/Provider.html">Template::Provider</a> objects (the
 
154
                      <code>LOAD_TEMPLATES</code> posse) who themselves might get involved with
 
155
                      a <a href="../modules/Template/Parser.html">Template::Parser</a> to
 
156
                      help turn source templates into executable Perl code (but more on that
 
157
                      later).
 
158
                    </p>
 
159
                    <p>
 
160
                      Thankfully, all of this complexity is hidden away behind a simple <a
 
161
                      href="#method_template">Template::Context#template()</a> method. You call
 
162
                      it passing a template name as an argument, and it returns a compiled
 
163
                      template in the form of a <a href="../modules/Template/Document.html">Template::Document</a> object, or otherwise raises an exception.
 
164
                    </p>
 
165
                    <p>
 
166
                      A <a href="../modules/Template/Document.html">Template::Document</a> is a thin object wrapper around a compiled
 
167
                      template subroutine. The object implements a <a
 
168
                      href="#method_process">Template::Document#process()</a> method which
 
169
                      performs a little bit of housekeeping and then calls the template
 
170
                      subroutine. The object also defines template metadata (defined in
 
171
                      <code>[% META ... %]</code> directives) and has a <a
 
172
                      href="#method_block">Template::Document#block()</a> method which returns
 
173
                      a hash of any additional <code>[% BLOCK xxxx %]</code> definitions found
 
174
                      in the template source.
 
175
                    </p>
 
176
                    <p>
 
177
                      So the context fetches a compiled document via its own <a
 
178
                      href="#method_template">Template::Context#template()</a> method and then
 
179
                      gets ready to process it. It first updates the stash (the place where
 
180
                      template variables get defined - more on that shortly) to set any
 
181
                      template variable definitions specified as the second argument by
 
182
                      reference to hash array. Then, it calls the document <a
 
183
                      href="#method_process">Template::Document#process()</a> method, passing a
 
184
                      reference to itself, the context object, as an argument. In doing this,
 
185
                      it provides itself as an object against which template code can make
 
186
                      callbacks to access runtime resources and Template Toolkit functionality.
 
187
                    </p>
 
188
                    <p>
 
189
                      What we're trying to say here is this: not only does the <a href="../modules/Template/Context.html">Template::Context</a> object receive
 
190
                      calls from the <i>outside</i>, i.e. those originating in user code
 
191
                      calling the process() method on a Template object, but it also receives
 
192
                      calls from the <i>inside</i>, i.e. those originating in template
 
193
                      directives of the form <code>[% PROCESS template %]</code>.
 
194
                    </p>
 
195
                    <p>
 
196
                      Before we move on to that, here's a simple structure diagram showing the
 
197
                      outer layers of the Template Toolkit heading inwards, with pseudo code
 
198
                      annotations showing a typical invocation sequence.
 
199
                    </p>
 
200
                    <pre> ,--------.
 
201
 | Caller |     use Template;
 
202
 `--------'     my $tt = Template-&gt;new( ... );
 
203
      |         $tt-&gt;process($template, \%vars);
 
204
      |                                                     Outside
 
205
- - - | - - - - - - - - - - - - - - - - - - - - - - - - - - - - T T 
 
206
      |         package Template;                            Inside
 
207
      V
 
208
+----------+    sub process($template, \%vars) {
 
209
| Template |        $out = $self-&gt;SERVICE-&gt;process($template, $vars);
 
210
+----------+        print $out or send it to $self-&gt;OUTPUT;
 
211
      |         }
 
212
      |
 
213
      |         package Template::Service;
 
214
      |
 
215
      |         sub process($template, \%vars) {
 
216
      |             try {
 
217
+----------+            foreach $p in @self-&gt;PRE_PROCESS
 
218
| Service  |                $self-&gt;CONTEXT-&gt;process($p, $vars);
 
219
+----------+
 
220
      |                 $self-&gt;CONTEXT-&gt;process($template, $vars);
 
221
      |
 
222
      |                 foreach $p @self-&gt;POST_PROCESS
 
223
      |                     $self-&gt;CONTEXT-&gt;process($p, $vars);
 
224
      |             }
 
225
      |             catch {
 
226
      |                 $self-&gt;CONTEXT-&gt;process($self-&gt;ERROR);
 
227
      |             }
 
228
      |         }
 
229
      |
 
230
      V         package Template::Context;
 
231
+----------+    
 
232
| Context  |    sub process($template, \%vars) {
 
233
+----------+        # fetch compiled template
 
234
      |             $template = $self-&gt;template($template)
 
235
      |             # update stash
 
236
      |             $self-&gt;STASH-&gt;update($vars);
 
237
      |             # process template
 
238
      |             $template-&gt;process($self)
 
239
      |         }
 
240
      V     
 
241
+----------+    package Template::Document;
 
242
| Document |    
 
243
+----------+    sub process($context) {
 
244
                    $output = &amp;{ $self-&gt;BLOCK }($context);
 
245
                }</pre>
 
246
              </div>
 
247
            </div>
 
248
            <div class="section">
 
249
              <div class="head">
 
250
                <h1 id="Inside_Looking_Out" onclick="switch_section(this)" title="Click title to show/hide section content.">Inside Looking Out</h1>
 
251
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
252
              </div>
 
253
              <div class="body">
 
254
                <p>
 
255
                      To understand more about what's going on in these lower levels, we need
 
256
                      to look at what a compiled template looks like. In fact, a compiled
 
257
                      template is just a regular Perl sub-routine. Here's a very simple one.
 
258
                    </p>
 
259
                    <pre>sub my_compiled_template {
 
260
    return "This is a compiled template.\n";
 
261
}</pre>
 
262
                    <p>
 
263
                      You're unlikely to see a compiled template this simple unless you wrote
 
264
                      it yourself but it is entirely valid. All a template subroutine is
 
265
                      obliged to do is return some output (which may be an empty of course). If
 
266
                      it can't for some reason, then it should raise an error via
 
267
                      <code>die()</code>.
 
268
                    </p>
 
269
                    <pre>sub my_todo_template {
 
270
    die "This template not yet implemented\n";
 
271
}</pre>
 
272
                    <p>
 
273
                      If it wants to get fancy, it can raise an error as a <a href="../modules/Template/Exception.html">Template::Exception</a> object. An
 
274
                      exception object is really just a convenient wrapper for the
 
275
                      '<code>type</code>' and '<code>info</code>' fields.
 
276
                    </p>
 
277
                    <pre>sub my_solilique_template {
 
278
    die (Template::Exception-&gt;new('yorrick', 'Fellow of infinite jest'));
 
279
}</pre>
 
280
                    <p>
 
281
                      Templates generally need to do a lot more than just generate static
 
282
                      output or raise errors. They may want to inspect variable values, process
 
283
                      another template, load a plugin, run a filter, and so on. Whenever a
 
284
                      template subroutine is called, it gets passed a reference to a <a
 
285
                      href="../modules/Template/Context.html">Template::Context</a>
 
286
                      object. It is through this context object that template code can access
 
287
                      the features of the Template Toolkit.
 
288
                    </p>
 
289
                    <p>
 
290
                      We described earlier how the <a href="../modules/Template/Service.html">Template::Service</a> object calls on <a href="../modules/Template/Context.html">Template::Context</a> to handle a <a
 
291
                      href="#method_process">Template::Context#process()</a> request from the
 
292
                      <i>outside</i>. We can make a similar request on a context to process a
 
293
                      template, but from within the code of another template. This is a call
 
294
                      from the <i>inside</i>.
 
295
                    </p>
 
296
                    <pre>sub my_process_template {
 
297
    my $context = shift;
 
298
    my $output = $context-&gt;process('header', { title =&gt; 'Hello World' })
 
299
               . "\nsome content\n"
 
300
               . $context-&gt;process('footer');
 
301
}</pre>
 
302
                    <p>
 
303
                      This is then roughly equivalent to a source template something like this:
 
304
                    </p>
 
305
                    <pre>[% PROCESS header
 
306
    title = 'Hello World'
 
307
%]
 
308
some content
 
309
[% PROCESS footer %]</pre>
 
310
                    <p>
 
311
                      Template variables are stored in, and managed by a <a href="../modules/Template/Stash.html">Template::Stash</a> object. This is a
 
312
                      blessed hash array in which template variables are defined. The object
 
313
                      wrapper provides <a href="#method_get">Template::Stash#get()</a> and <a
 
314
                      href="#method_set">Template::Stash#set()</a> method which implement all
 
315
                      the <i>magical.variable.features</i> of the Template Toolkit.
 
316
                    </p>
 
317
                    <p>
 
318
                      Each context object has its own stash, a reference to which can be
 
319
                      returned by the appropriately named <a
 
320
                      href="#method_stash">Template::Context#stash()</a> method. So to print
 
321
                      the value of some template variable, or for example, to represent the
 
322
                      following source template:
 
323
                    </p>
 
324
                    <pre>&lt;title&gt;[% title %]&lt;/title&gt;</pre>
 
325
                    <p>
 
326
                      we might have a subroutine definition something like this:
 
327
                    </p>
 
328
                    <pre>sub {
 
329
    my $context = shift;
 
330
    my $stash = $context-&gt;stash();
 
331
    return '&lt;title&gt;' . $stash-&gt;get('title') . '&lt;/title&gt;';
 
332
}</pre>
 
333
                    <p>
 
334
                      The stash <a href="#method_get">Template::Stash#get()</a> method hides
 
335
                      the details of the underlying variable types, automatically calling code
 
336
                      references, checking return values, and performing other such tricks. If
 
337
                      '<code>title</code>' happens to be bound to a subroutine then we can
 
338
                      specify additional parameters as a list reference passed as the second
 
339
                      argument to get().
 
340
                    </p>
 
341
                    <pre>[% title('The Cat Sat on the Mat') %]</pre>
 
342
                    <p>
 
343
                      This translates to the stash call:
 
344
                    </p>
 
345
                    <pre>$stash-&gt;get([ 'title', ['The Cat Sat on the Mat'] ]);</pre>
 
346
                    <p>
 
347
                      Dotted compound variables can be requested by passing a single list
 
348
                      reference to the <code>get()</code> method in place of the variable name.
 
349
                      Each pair of elements in the list should correspond to the variable name
 
350
                      and reference to a list of arguments for each dot-delimited element of
 
351
                      the variable.
 
352
                    </p>
 
353
                    <pre>[% foo(1, 2).bar(3, 4).baz(5) %]</pre>
 
354
                    <p>
 
355
                      is thus equivalent to
 
356
                    </p>
 
357
                    <pre>$stash-&gt;get([ foo =&gt; [1,2], bar =&gt; [3,4], baz =&gt; [5] ]);</pre>
 
358
                    <p>
 
359
                      If there aren't any arguments for an element, you can specify an empty,
 
360
                      zero or null argument list.
 
361
                    </p>
 
362
                    <pre>[% foo.bar %]
 
363
$stash-&gt;get([ 'foo', 0, 'bar', 0 ]);</pre>
 
364
                    <p>
 
365
                      The <a href="#method_set">Template::Stash#set()</a> method works in a
 
366
                      similar way. It takes a variable name and a variable value which should
 
367
                      be assigned to it.
 
368
                    </p>
 
369
                    <pre>[% x = 10 %]         
 
370
$stash-&gt;set('x', 10);
 
371
 
 
372
[% x.y = 10 %]
 
373
$stash-&gt;set([ 'x', 0, 'y', 0 ], 10);</pre>
 
374
                    <p>
 
375
                      So the stash gives us access to template variables and the context
 
376
                      provides the higher level functionality.
 
377
                    </p>
 
378
                    <p>
 
379
                      Alongside the <a href="#method_process">Template::Context#process()</a>
 
380
                      method lies the <a href="#method_include">Template::Context#include()</a>
 
381
                      method. Just as with the <code>PROCESS</code> / <code>INCLUDE</code>
 
382
                      directives, the key difference is in variable localisation. Before
 
383
                      processing a template, the <code>process()</code> method simply updates
 
384
                      the stash to set any new variable definitions, overwriting any existing
 
385
                      values. In contrast, the <code>include()</code> method creates a copy of
 
386
                      the existing stash, in a process known as <i>cloning</i> the stash, and
 
387
                      then uses that as a temporary variable store. Any previously existing
 
388
                      variables are still defined, but any changes made to variables, including
 
389
                      setting the new variable values passed aas arguments will affect only the
 
390
                      local copy of the stash (although note that it's only a shallow copy, so
 
391
                      it's not foolproof). When the template has been processed, the
 
392
                      <code>include()</code> method restores the previous variable state by
 
393
                      <i>decloning</i> the stash.
 
394
                    </p>
 
395
                    <p>
 
396
                      The context also provides an <a
 
397
                      href="#method_insert">Template::Context#insert()</a> method to implement
 
398
                      the <code>INSERT</code> directive, but no <code>wrapper()</code> method.
 
399
                      This functionality can be implemented by rewriting the Perl code and
 
400
                      calling <code>include()</code>.
 
401
                    </p>
 
402
                    <pre>[% WRAPPER foo -%]
 
403
   blah blah [% x %]
 
404
[%- END %]
 
405
 
 
406
$context-&gt;include('foo', {
 
407
    content =&gt; 'blah blah ' . $stash-&gt;get('x'),
 
408
});</pre>
 
409
                    <p>
 
410
                      Other than the template processing methods <code>process()</code>,
 
411
                      <code>include()</code> and <code>insert()</code>, the context defines
 
412
                      methods for fetching plugin objects, <a
 
413
                      href="#method_plugin">Template::Context#plugin()</a>, and filters, <a
 
414
                      href="#method_filter">Template::Context#filter()</a>.
 
415
                    </p>
 
416
                    <pre># TT USE directive
 
417
[% USE foo = Bar(10) %]
 
418
 
 
419
# equivalent Perl
 
420
$stash-&gt;set('foo', $context-&gt;plugin('Bar', [10]));</pre>
 
421
                    <pre># TT FILTER block
 
422
[% FILTER bar(20) %]
 
423
   blah blah blah
 
424
[% END %]
 
425
 
 
426
# equivalent Perl
 
427
my $filter = $context-&gt;filter('bar', [20]);
 
428
&amp;$filter('blah blah blah');</pre>
 
429
                    <p>
 
430
                      Pretty much everything else you might want to do in a template can be
 
431
                      done in Perl code. Things like <code>IF</code>, <code>UNLESS</code>,
 
432
                      <code>FOREACH</code> and so on all have direct counterparts in Perl.
 
433
                    </p>
 
434
                    <pre># TT IF directive
 
435
[% IF msg %]
 
436
   Message: [% msg %]
 
437
[% END %];</pre>
 
438
                    <pre># equivalent Perl
 
439
if ($stash-&gt;get('msg')) {
 
440
    $output .=  'Message: ';
 
441
    $output .= $stash-&gt;get('msg');
 
442
}</pre>
 
443
                    <p>
 
444
                      The best way to get a better understanding of what's going on underneath
 
445
                      the hood is to set the <code>$Template::Parser::DEBUG</code> flag to a
 
446
                      true value and start processing templates. This will cause the parser to
 
447
                      print the generated Perl code for each template it compiles to
 
448
                      <code>STDERR</code>. You'll probably also want to set the
 
449
                      <code>$Template::Directive::PRETTY</code> option to have the Perl
 
450
                      pretty-printed for human consumption.
 
451
                    </p>
 
452
                    <pre>use Template;
 
453
use Template::Parser;
 
454
use Template::Directive;
 
455
 
 
456
$Template::Parser::DEBUG = 1;
 
457
$Template::Directive::PRETTY = 1;
 
458
 
 
459
my $template = Template-&gt;new();
 
460
$template-&gt;process(\*DATA, { cat =&gt; 'dog', mat =&gt; 'log' });
 
461
 
 
462
__DATA__
 
463
The [% cat %] sat on the [% mat %]</pre>
 
464
                    <p>
 
465
                      The output sent to <code>STDOUT</code> remains as you would expect:
 
466
                    </p>
 
467
                    <pre>The dog sat on the log</pre>
 
468
                    <p>
 
469
                      The output sent to <code>STDERR</code> would look something like this:
 
470
                    </p>
 
471
                    <pre>compiled main template document block:
 
472
sub {
 
473
    my $context = shift || die "template sub called without context\n";
 
474
    my $stash   = $context-&gt;stash;
 
475
    my $output  = '';
 
476
    my $error;
 
477
 
 
478
    eval { BLOCK: {
 
479
        $output .=  "The ";
 
480
        $output .=  $stash-&gt;get('cat');
 
481
        $output .=  " sat on the ";
 
482
        $output .=  $stash-&gt;get('mat');
 
483
        $output .=  "\n";
 
484
    } };
 
485
    if ($@) {
 
486
        $error = $context-&gt;catch($@, \$output);
 
487
        die $error unless $error-&gt;type eq 'return';
 
488
    }
 
489
 
 
490
    return $output;
 
491
}</pre>
 
492
              </div>
 
493
            </div>
 
494
            <div class="section">
 
495
              <div class="head">
 
496
                <h1 id="Hacking_on_the_Template_Toolkit" onclick="switch_section(this)" title="Click title to show/hide section content.">Hacking on the Template Toolkit</h1>
 
497
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
 
498
              </div>
 
499
              <div class="body">
 
500
                <p>
 
501
                      Please feel free to hack on the Template Toolkit. If you find a bug that
 
502
                      needs fixing, if you have an idea for something that's missing, or you
 
503
                      feel inclined to tackle something on the TODO list, then by all means go
 
504
                      ahead and do it!
 
505
                    </p>
 
506
                    <p>
 
507
                      If you're contemplating something non-trivial then you'll probably want
 
508
                      to bring it up on the mailing list first to get an idea about the current
 
509
                      state of play, find out if anyone's already working on it, and so on.
 
510
                    </p>
 
511
                    <p>
 
512
                      When you start to hack on the Template Toolkit, please make sure you
 
513
                      start from the latest developer release. Stable releases are uploaded to
 
514
                      CPAN and have all-numerical version numbers, e.g. 2.04, 2.05. Developer
 
515
                      releases are available from the Template Toolkit web site and have a
 
516
                      character suffix on the version, e.g. 2.04a, 2.04b, etc.
 
517
                    </p>
 
518
                    <p>
 
519
                      Once you've made your changes, please remember to update the test suite
 
520
                      by adding extra tests to one of the existing test scripts in the
 
521
                      <code>t</code> sub-directory, or by adding a new test script of your own.
 
522
                      And of course, run <code>make test</code> to ensure that all the tests
 
523
                      pass with your new code.
 
524
                    </p>
 
525
                    <p>
 
526
                      Don't forget that any files you do add will need to be added to the
 
527
                      MANIFEST. Running <code>make manifest</code> will do this for you, but
 
528
                      you need to make sure you haven't got any other temporary files lying
 
529
                      around that might also get added to it.
 
530
                    </p>
 
531
                    <p>
 
532
                      Documentation is often something that gets overlooked but it's just as
 
533
                      important as the code. If you're adding a new module, a plugin module,
 
534
                      for example, then it's OK to include the POD documentation in with the
 
535
                      module, but <i>please</i> write it all in one piece at the end of the
 
536
                      file, <i>after</i> the code (just look at any other
 
537
                      <code>Template::*</code> module for an example). It's a religious issue,
 
538
                      I know, but I have a strong distaste for POD documentation interspersed
 
539
                      throughout the code. In my not-so-humble opinion, it makes both the code
 
540
                      and the documentation harder to read (same kinda problem as embedding
 
541
                      Perl in HTML).
 
542
                    </p>
 
543
                    <p>
 
544
                      To share your changes with the rest of the world, you'll need to prepare
 
545
                      a patch file. To do this you should have 2 directories side-by-side, one
 
546
                      which is the original, unmodified distribution directory for the latest
 
547
                      developer release, and the other is a copy of that same directory which
 
548
                      includes your changes.
 
549
                    </p>
 
550
                    <p>
 
551
                      The following example shows a typical hacking session. First we unpack
 
552
                      the latest developer release.
 
553
                    </p>
 
554
                    <pre>$ tar zxf Template-Toolkit-2.05c.tar.gz</pre>
 
555
                    <p>
 
556
                      At this point, it's a good idea to rename the directory to give some
 
557
                      indicate of what it contains.
 
558
                    </p>
 
559
                    <pre>$ mv Template-Toolkit-2.05c Template-Toolkit-2.05c-abw-xyz-hack</pre>
 
560
                    <p>
 
561
                      Then go hack!
 
562
                    </p>
 
563
                    <pre>$ cd Template-Toolkit-2.05c-abw-xyz-hack</pre>
 
564
                    <pre>[ hacking ]</pre>
 
565
                    <pre>$ cd ..</pre>
 
566
                    <p>
 
567
                      When you're all done and ready to prepare a patch, unpack the
 
568
                      distribution archive again so that you've got the original to
 
569
                      <code>diff</code> against your new code.
 
570
                    </p>
 
571
                    <pre>$ tar zxf Template-Toolkit-2.05c.tar.gz</pre>
 
572
                    <p>
 
573
                      You should now have an original distribution directory and a modified
 
574
                      version of that same directory, side-by-side.
 
575
                    </p>
 
576
                    <pre>$ ls
 
577
Template-Toolkit-2.05c  Template-Toolkit-2.05c-abw-xyz-hack</pre>
 
578
                    <p>
 
579
                      Now run <code>diff</code> and save the output into an appropriately named
 
580
                      patch file.
 
581
                    </p>
 
582
                    <pre>$ diff -Naur Template-Toolkit-2.05c Template-Toolkit-2.05c-abw-xyz-hack &gt; patch-TT205c-abw-xyz-hack</pre>
 
583
                    <p>
 
584
                      You can then post the generated patch file to the mailing list,
 
585
                      describing what it does, why it does it, how it does it and any other
 
586
                      relevant information.
 
587
                    </p>
 
588
                    <p>
 
589
                      If you want to apply someone else's patch then you should start with the
 
590
                      same original distribution source on which the patch is based. From
 
591
                      within the root of the distribution, run <code>patch</code> feeding in
 
592
                      the patch file as standard input. The '<code>p1</code>' option is
 
593
                      required to strip the first element of the path name (e.g.
 
594
                      <code>Template-Toolkit-2.05c/README</code> becomes <code>README</code>
 
595
                      which is then the correct path).
 
596
                    </p>
 
597
                    <pre>$ tar zxf Template-Toolkit-2.05c.tar.gz
 
598
$ cd Template-Toolkit-2.05c
 
599
$ patch -p1 &lt; ../patch-TT205c-abw-xyz-hack</pre>
 
600
                    <p>
 
601
                      The output generated by <code>patch</code> should be something like the
 
602
                      following:
 
603
                    </p>
 
604
                    <pre>patching file README
 
605
patching file lib/Template.pm
 
606
patching file lib/Template/Provider.pm
 
607
patching file t/provider.t</pre>
 
608
              </div>
 
609
            </div>
 
610
            
 
611
            </div></div>
 
612
          <br class="clear" />
 
613
          <div class="pageinfo">
 
614
            /manual/Internals.html last modified 10:57:40 31-May-2007
 
615
          </div>
 
616
        </div>
 
617
        
 
618
        <div id="footer">
 
619
          <a href="http://opensource.org/" class="osi"></a>
 
620
          <div class="controls">
 
621
          <div class="pager">
 
622
            <a href="../manual/Plugins.html" title="Template::Manual::Plugins" class="go back">Back<span class="about"><h4>Template::Manual::Plugins</h4></span></a>
 
623
            <a href="../manual/index.html" title="Template::Manual" class="go up">Up<span class="about"><h4>Template::Manual</h4></span></a>
 
624
            <a href="../manual/Views.html" title="Template::Manual::Views" class="go next">Next<span class="about"><h4>Template::Manual::Views</h4></span></a>
 
625
          </div>
 
626
          </div>
 
627
          <div class="copyright">
 
628
            Copyright &copy; 1996-2007 <a href="http://wardley.org/">Andy Wardley</a>.  All Rights Reserved.
 
629
          </div>
 
630
          <div class="licence">
 
631
            The <a href="http://template-toolkit.org/">Template Toolkit</a> is <a href="http://opensource.org/">Open Source</a> software.
 
632
            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>
 
633
            or the <a href="http://www.opensource.org/licenses/artistic-license.php">Perl Artistic Licence</a>.
 
634
          </div>
 
635
        </div>
 
636
        <div id="palette">
 
637
          <ul>
 
638
            <li class="first"><a href="#" class="blue" onclick="set_style('Clear Blue')"></a></li>
 
639
            <li><a href="#" class="orange" onclick="set_style('Clear Orange')"></a></li>
 
640
            <li><a href="#" class="green" onclick="set_style('Clear Green')"></a></li>
 
641
            <li><a href="#" class="purple" onclick="set_style('Clear Purple')"></a></li>
 
642
            <li><a href="#" class="grey" onclick="set_style('Clear Grey')"></a></li>
 
643
          </ul>
 
644
        </div>
 
645
    </div>  </body>
 
646
</html>