3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
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">
13
<link rel="stylesheet" type="text/css" href="../css/ie6.css" />
15
<link rel="stylesheet" type="text/css" href="/css/print.css" media="print">
16
<script type="text/javascript" src="../js/tt2.js"></script>
17
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
18
<meta name="author" content="Andy Wardley">
23
<a href="../index.html" id="logo" alt="" title="Click for the Home Page"><span class="alt">TT2 Home Page</span></a>
25
<li><a href="../manual/index.html">Manual</a></li>
26
<li class="last"><a href="../manual/Internals.html">Internals</a></li>
28
<div class="controls">
29
<a href="#" class="menu show" onclick="widescreen_off(); return false" title="Show Menu">
30
<span class="about">Click to view the menu. It's very nice.</span>
32
<a href="#" class="menu hide" onclick="widescreen_on(); return false" title="Hide Menu">
33
<span class="about">Click to hide the menu and go all widescreen!</span>
37
<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>
42
<h1 class="headline">Template::Manual::Internals</h1>
43
<h2 class="subhead">Template Toolkit internals</h1>
48
<a href="../index.html" id="logo"></a>
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>
67
<div class="foot"></div>
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>
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>
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>
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>
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.
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.
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.
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.
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.
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
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.
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.
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.
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>.
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.
201
| Caller | use Template;
202
`--------' my $tt = Template->new( ... );
203
| $tt->process($template, \%vars);
205
- - - | - - - - - - - - - - - - - - - - - - - - - - - - - - - - T T
206
| package Template; Inside
208
+----------+ sub process($template, \%vars) {
209
| Template | $out = $self->SERVICE->process($template, $vars);
210
+----------+ print $out or send it to $self->OUTPUT;
213
| package Template::Service;
215
| sub process($template, \%vars) {
217
+----------+ foreach $p in @self->PRE_PROCESS
218
| Service | $self->CONTEXT->process($p, $vars);
220
| $self->CONTEXT->process($template, $vars);
222
| foreach $p @self->POST_PROCESS
223
| $self->CONTEXT->process($p, $vars);
226
| $self->CONTEXT->process($self->ERROR);
230
V package Template::Context;
232
| Context | sub process($template, \%vars) {
233
+----------+ # fetch compiled template
234
| $template = $self->template($template)
236
| $self->STASH->update($vars);
238
| $template->process($self)
241
+----------+ package Template::Document;
243
+----------+ sub process($context) {
244
$output = &{ $self->BLOCK }($context);
248
<div class="section">
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>
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.
259
<pre>sub my_compiled_template {
260
return "This is a compiled template.\n";
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
269
<pre>sub my_todo_template {
270
die "This template not yet implemented\n";
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.
277
<pre>sub my_solilique_template {
278
die (Template::Exception->new('yorrick', 'Fellow of infinite jest'));
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.
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>.
296
<pre>sub my_process_template {
298
my $output = $context->process('header', { title => 'Hello World' })
300
. $context->process('footer');
303
This is then roughly equivalent to a source template something like this:
305
<pre>[% PROCESS header
306
title = 'Hello World'
309
[% PROCESS footer %]</pre>
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.
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:
324
<pre><title>[% title %]</title></pre>
326
we might have a subroutine definition something like this:
330
my $stash = $context->stash();
331
return '<title>' . $stash->get('title') . '</title>';
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
341
<pre>[% title('The Cat Sat on the Mat') %]</pre>
343
This translates to the stash call:
345
<pre>$stash->get([ 'title', ['The Cat Sat on the Mat'] ]);</pre>
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
353
<pre>[% foo(1, 2).bar(3, 4).baz(5) %]</pre>
355
is thus equivalent to
357
<pre>$stash->get([ foo => [1,2], bar => [3,4], baz => [5] ]);</pre>
359
If there aren't any arguments for an element, you can specify an empty,
360
zero or null argument list.
363
$stash->get([ 'foo', 0, 'bar', 0 ]);</pre>
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
370
$stash->set('x', 10);
373
$stash->set([ 'x', 0, 'y', 0 ], 10);</pre>
375
So the stash gives us access to template variables and the context
376
provides the higher level functionality.
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.
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>.
402
<pre>[% WRAPPER foo -%]
406
$context->include('foo', {
407
content => 'blah blah ' . $stash->get('x'),
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>.
416
<pre># TT USE directive
417
[% USE foo = Bar(10) %]
420
$stash->set('foo', $context->plugin('Bar', [10]));</pre>
421
<pre># TT FILTER block
427
my $filter = $context->filter('bar', [20]);
428
&$filter('blah blah blah');</pre>
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.
434
<pre># TT IF directive
438
<pre># equivalent Perl
439
if ($stash->get('msg')) {
440
$output .= 'Message: ';
441
$output .= $stash->get('msg');
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.
453
use Template::Parser;
454
use Template::Directive;
456
$Template::Parser::DEBUG = 1;
457
$Template::Directive::PRETTY = 1;
459
my $template = Template->new();
460
$template->process(\*DATA, { cat => 'dog', mat => 'log' });
463
The [% cat %] sat on the [% mat %]</pre>
465
The output sent to <code>STDOUT</code> remains as you would expect:
467
<pre>The dog sat on the log</pre>
469
The output sent to <code>STDERR</code> would look something like this:
471
<pre>compiled main template document block:
473
my $context = shift || die "template sub called without context\n";
474
my $stash = $context->stash;
480
$output .= $stash->get('cat');
481
$output .= " sat on the ";
482
$output .= $stash->get('mat');
486
$error = $context->catch($@, \$output);
487
die $error unless $error->type eq 'return';
494
<div class="section">
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>
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
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.
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.
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.
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.
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
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.
551
The following example shows a typical hacking session. First we unpack
552
the latest developer release.
554
<pre>$ tar zxf Template-Toolkit-2.05c.tar.gz</pre>
556
At this point, it's a good idea to rename the directory to give some
557
indicate of what it contains.
559
<pre>$ mv Template-Toolkit-2.05c Template-Toolkit-2.05c-abw-xyz-hack</pre>
563
<pre>$ cd Template-Toolkit-2.05c-abw-xyz-hack</pre>
564
<pre>[ hacking ]</pre>
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.
571
<pre>$ tar zxf Template-Toolkit-2.05c.tar.gz</pre>
573
You should now have an original distribution directory and a modified
574
version of that same directory, side-by-side.
577
Template-Toolkit-2.05c Template-Toolkit-2.05c-abw-xyz-hack</pre>
579
Now run <code>diff</code> and save the output into an appropriately named
582
<pre>$ diff -Naur Template-Toolkit-2.05c Template-Toolkit-2.05c-abw-xyz-hack > patch-TT205c-abw-xyz-hack</pre>
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.
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).
597
<pre>$ tar zxf Template-Toolkit-2.05c.tar.gz
598
$ cd Template-Toolkit-2.05c
599
$ patch -p1 < ../patch-TT205c-abw-xyz-hack</pre>
601
The output generated by <code>patch</code> should be something like the
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>
613
<div class="pageinfo">
614
/manual/Internals.html last modified 10:57:40 31-May-2007
619
<a href="http://opensource.org/" class="osi"></a>
620
<div class="controls">
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>
627
<div class="copyright">
628
Copyright © 1996-2007 <a href="http://wardley.org/">Andy Wardley</a>. All Rights Reserved.
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>.
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>