Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / i486-linux-gnu-thread-multi / Template / Manual / Intro.pod
CommitLineData
3fea05b9 1#============================================================= -*-perl-*-
2#
3# Template::Manual::Intro
4#
5# AUTHOR
6# Andy Wardley <abw@wardley.org>
7#
8# COPYRIGHT
9# Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
10#
11# This module is free software; you can redistribute it and/or
12# modify it under the same terms as Perl itself.
13#
14#========================================================================
15
16=head1 NAME
17
18Template::Manual::Intro - Introduction to the Template Toolkit
19
20=head1 Introduction
21
22The Template Toolkit is a collection of Perl modules which implement a
23fast, flexible, powerful and extensible template processing system.
24It is most often used for generating dynamic web content, although it can
25be used equally well for processing any kind of text documents.
26
27At the simplest level it provides an easy way to process template
28files, filling in embedded variable references with their equivalent
29values. Here's an example of a template.
30
31 Dear [% name %],
32
33 It has come to our attention that your account is in
34 arrears to the sum of [% debt %].
35
36 Please settle your account before [% deadline %] or we
37 will be forced to revoke your Licence to Thrill.
38
39 The Management.
40
41By default, template directives are embedded within the character
42sequences C<[%> ... C<%]> but you can change these and various other
43options to configure how the Template Toolkit looks, feels and works.
44You can set the C<INTERPOLATE> option, for example, if you prefer to
45embed your variables in Perl style:
46
47 Dear $name,
48
49 It has come to our attention that your account is in
50 arrears to the sum of $debt.
51
52 ...etc...
53
54=head1 The Template Perl Module
55
56The L<Template> Perl module is the front end to the Template Toolkit for Perl
57programmers, providing access to the full range of functionality through a
58single module with a simple interface. It loads the other modules as required
59and instantiates a default set of objects to handle subsequent template
60processing requests. Configuration parameters may be passed to the L<Template>
61constructor method, L<new()|Template#new()>, which are then used to
62configure the generate object.
63
64 use Template;
65
66 my $tt = Template->new({
67 INCLUDE_PATH => '/usr/local/templates',
68 INTERPOLATE => 1,
69 }) || die "$Template::ERROR\n";
70
71The L<Template> object implements a L<process()|Template#process()> method for
72processing template files or text. The name of the input template (or various
73other sources) is passed as the first argument, followed by a reference to a
74hash array of variable definitions for substitution in the template.
75
76 my $vars = {
77 name => 'Count Edward van Halen',
78 debt => '3 riffs and a solo',
79 deadline => 'the next chorus',
80 };
81
82 $tt->process('letters/overdrawn', $vars)
83 || die $tt->error(), "\n";
84
85The L<process()|Template#process()> method returns a true value (C<1>) on success
86and prints the template output to C<STDOUT>, by default. On error, the
87L<process()|Template#process()> method returns a false value (C<undef>).
88The L<error()|Template#error()> method can then be called to retrieve
89details of the error.
90
91=head1 Component Based Content Construction
92
93A number of special directives are provided, such as C<INSERT>, C<INCLUDE> and
94C<PROCESS>, which allow content to be built up from smaller template
95components. This permits a modular approach to building a web site or other
96content repository, promoting reusability, cross-site consistency, ease of
97construction and subsequent maintenance. Common elements such as headers,
98footers, menu bars, tables, and so on, can be created as separate template
99files which can then be processed into other documents as required. All
100defined variables are inherited by these templates along with any additional
101"local" values specified.
102
103 [% PROCESS header
104 title = "The Cat Sat on the Mat"
105 %]
106
107 [% PROCESS menu %]
108
109 The location of the missing feline has now been established.
110 Thank you for your assistance.
111
112 [% INSERT legal/disclaimer %]
113
114 [% PROCESS footer %]
115
116You can also define a template as a BLOCK within the same file and
117PROCESS it just like any other template file. This can be invaluable
118for building up repetitive elements such as tables, menus, etc.
119
120 [% BLOCK tabrow %]
121 <tr><td>[% name %]</td><td>[% email %]</td></tr>
122 [% END %]
123
124 <table>
125 [% PROCESS tabrow name="tom" email="tom@here.org" %]
126 [% PROCESS tabrow name="dick" email="disk@there.org" %]
127 [% PROCESS tabrow name="larry" email="larry@where.org" %]
128 </table>
129
130=head1 Data and Code Binding
131
132One of the key features that sets the Template Toolkit apart from
133other template processors is the ability to bind template variables to
134any kind of Perl data: scalars, lists, hash arrays, sub-routines and
135objects.
136
137 my $vars = {
138 root => 'http://here.com/there',
139 menu => [ 'modules', 'authors', 'scripts' ],
140 client => {
141 name => 'Doctor Joseph von Satriani',
142 id => 'JVSAT',
143 },
144 checkout => sub { my $total = shift; ...; return $something },
145 shopcart => My::Cool::Shopping::Cart->new(),
146 };
147
148The Template Toolkit will automatically Do The Right Thing to access the data
149in an appropriate manner to return some value which can then be output. The
150dot operator 'C<.>' is used to access into lists and hashes or to call object
151methods. The C<FOREACH> directive is provided for iterating through lists, and
152various logical tests are available using directives such as C<IF>, C<UNLESS>,
153C<ELSIF>, C<ELSE>, C<SWITCH>, C<CASE>, etc.
154
155 [% FOREACH section = menu %]
156 <a href="[% root %]/[% section %]/index.html">[% section %]</a>
157 [% END %]
158
159 <b>Client</a>: [% client.name %] (id: [% client.id %])
160
161 [% IF shopcart.nitems %]
162 Your shopping cart contains the following items:
163 <ul>
164 [% FOREACH item = shopcart.contents %]
165 <li>[% item.name %] : [% item.qty %] @ [% item.price %]
166 [% END %]
167 </ul>
168
169 [% checkout(shopcart.total) %]
170
171 [% ELSE %]
172 No items currently in shopping cart.
173 [% END %]
174
175=head1 Advanced Features: Filters, Macros, Exceptions, Plugins
176
177The Template Toolkit also provides a number of additional directives
178for advanced processing and programmatical functionality. It supports
179output filters (FILTER), allows custom macros to be defined (MACRO),
180has a fully-featured exception handling system (TRY, THROW, CATCH,
181FINAL) and supports a plugin architecture (USE) which allows special
182plugin modules and even regular Perl modules to be loaded and used
183with the minimum of fuss. The Template Toolkit is "just" a template
184processor but you can trivially extend it to incorporate the
185functionality of any Perl module you can get your hands on. Thus, it
186is also a scalable and extensible template framework, ideally suited
187for managing the presentation layer for application servers, content
188management systems and other web applications.
189
190=head1 Separating Presentation and Application Logic
191
192Rather than embedding Perl code or some other scripting language
193directly into template documents, it encourages you to keep functional
194components (i.e. Perl code) separate from presentation components
195(e.g. HTML templates). The template variables provide the interface
196between the two layers, allowing data to be generated in code and then
197passed to a template component for displaying (pipeline model) or for
198sub-routine or object references to be bound to variables which can
199then be called from the template as and when required (callback
200model).
201
202The directives that the Template Toolkit provide implement their own
203mini programming language, but they're not really designed for
204serious, general purpose programming. Perl is a far more appropriate
205language for that. If you embed application logic (e.g. Perl or other
206scripting language fragments) in HTML templates then you risk losing
207the clear separation of concerns between functionality and
208presentation. It becomes harder to maintain the two elements in
209isolation and more difficult, if not impossible, to reuse code or
210presentation elements by themselves. It is far better to write your
211application code in separate Perl modules, libraries or scripts and
212then use templates to control how the resulting data is presented as
213output. Thus you should think of the Template Toolkit language as a
214set of layout directives for displaying data, not calculating it.
215
216Having said that, the Template Toolkit doesn't force you into one
217approach or the other. It attempts to be pragmatic rather than
218dogmatic in allowing you to do whatever best gets the job done.
219Thus, if you enable the EVAL_PERL option then you can happily embed
220real Perl code in your templates within PERL ... END directives.
221
222=head1 Performance
223
224The Template Toolkit uses a fast YACC-like parser which compiles
225templates into Perl code for maximum runtime efficiency. It also has
226an advanced caching mechanism which manages in-memory and on-disk
227(i.e. persistent) versions of compiled templates. The modules that
228comprise the toolkit are highly configurable and the architecture
229around which they're built is designed to be extensible. The Template
230Toolkit provides a powerful framework around which content creation
231and delivery systems can be built while also providing a simple
232interface through the Template front-end module for general use.
233
234=cut
235
236# Local Variables:
237# mode: perl
238# perl-indent-level: 4
239# indent-tabs-mode: nil
240# End:
241#
242# vim: expandtab shiftwidth=4: