1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
4 .\" ========================================================================
5 .de Sh \" Subsection heading
13 .de Sp \" Vertical space (when we can't use .PP)
17 .de Vb \" Begin verbatim text
22 .de Ve \" End verbatim text
26 .\" Set up some character translations and predefined strings. \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote. | will give a
29 .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
37 . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD. Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
57 . tm Index:\\$1\t\\n%\t"\\$2"
63 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear. Run. Save yourself. No user-serviceable parts.
70 . \" fudge factors for nroff and troff
79 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
85 . \" simple accents for nroff and troff
95 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
102 . \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 . \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 . \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
129 .\" ========================================================================
131 .IX Title "Template::Plugin 3"
132 .TH Template::Plugin 3 "2008-11-13" "perl v5.8.7" "User Contributed Perl Documentation"
134 Template::Plugin \- Base class for Template Toolkit plugins
136 .IX Header "SYNOPSIS"
138 \& package MyOrg::Template::Plugin::MyPlugin;
139 \& use base qw( Template::Plugin );
140 \& use Template::Plugin;
146 \& my $class = shift;
147 \& my $context = shift;
154 .IX Header "DESCRIPTION"
155 A \*(L"plugin\*(R" for the Template Toolkit is simply a Perl module which
156 exists in a known package location (e.g. \f(CW\*(C`Template::Plugin::*\*(C'\fR) and
157 conforms to a regular standard, allowing it to be loaded and used
160 The \f(CW\*(C`Template::Plugin\*(C'\fR module defines a base class from which other
161 plugin modules can be derived. A plugin does not have to be derived
162 from Template::Plugin but should at least conform to its object-oriented
165 It is recommended that you create plugins in your own package namespace
166 to avoid conflict with toolkit plugins. e.g.
169 \& package MyOrg::Template::Plugin::FooBar;
172 Use the \s-1PLUGIN_BASE\s0 option to specify
173 the namespace that you use. e.g.
177 \& my $template = Template\->new({
178 \& PLUGIN_BASE => 'MyOrg::Template::Plugin',
183 The following methods form the basic interface between the Template
184 Toolkit and plugin modules.
186 .IX Subsection "load($context)"
187 This method is called by the Template Toolkit when the plugin module
188 is first loaded. It is called as a package method and thus implicitly
189 receives the package name as the first parameter. A reference to the
190 Template::Context object loading the plugin is also passed. The
191 default behaviour for the \f(CW\*(C`load()\*(C'\fR method is to simply return the class
192 name. The calling context then uses this class name to call the \f(CW\*(C`new()\*(C'\fR
200 \& sub load { # called as MyPlugin\->load($context)
201 \& my ($class, $context) = @_;
202 \& return $class; # returns 'MyPlugin'
205 .ie n .Sh "new($context, @params)"
206 .el .Sh "new($context, \f(CW@params\fP)"
207 .IX Subsection "new($context, @params)"
208 This method is called to instantiate a new plugin object for the \f(CW\*(C`USE\*(C'\fR
209 directive. It is called as a package method against the class name returned by
210 \&\fIload()\fR. A reference to the Template::Context object creating the plugin
211 is passed, along with any additional parameters specified in the \f(CW\*(C`USE\*(C'\fR
215 \& sub new { # called as MyPlugin\->new($context)
216 \& my ($class, $context, @params) = @_;
218 \& _CONTEXT => $context,
219 \& }, $class; # returns blessed MyPlugin object
223 .IX Subsection "error($error)"
224 This method, inherited from the Template::Base module, is used for
225 reporting and returning errors. It can be called as a package method
226 to set/return the \f(CW$ERROR\fR package variable, or as an object method to
227 set/return the object \f(CW\*(C`_ERROR\*(C'\fR member. When called with an argument, it
228 sets the relevant variable and returns \f(CW\*(C`undef.\*(C'\fR When called without an
229 argument, it returns the value of the variable.
233 \& use base 'Template::Plugin';
238 \& my ($class, $context, $dsn) = @_;
242 \& return $class\->error('No data source specified')
258 \& my $something = MyPlugin\->new()
259 \& || die MyPlugin\->error(), "\en";
263 \& $something\->do_something()
264 \& || die $something\->error(), "\en";
267 .IX Header "DEEPER MAGIC"
268 The Template::Context object that handles the loading and use of plugins
269 calls the \fInew()\fR and \fIerror()\fR methods against the package name returned by
270 the \fIload()\fR method. In pseudo-code terms looks something like this:
273 \& $class = MyPlugin\->load($context); # returns 'MyPlugin'
277 \& $object = $class\->new($context, @params) # MyPlugin\->new(...)
278 \& || die $class\->error(); # MyPlugin\->error()
281 The \fIload()\fR method may alterately return a blessed reference to an
282 object instance. In this case, \fInew()\fR and \fIerror()\fR are then called as
283 \&\fIobject\fR methods against that prototype instance.
286 \& package YourPlugin;
291 \& my ($class, $context) = @_;
293 \& _CONTEXT => $context,
300 \& my ($self, $context, @params) = @_;
305 In this example, we have implemented a 'Singleton' plugin. One object
306 gets created when \fIload()\fR is called and this simply returns itself for
307 each call to \fInew()\fR.
309 Another implementation might require individual objects to be created
310 for every call to \fInew()\fR, but with each object sharing a reference to
311 some other object to maintain cached data, database handles, etc.
312 This pseudo-code example demonstrates the principle.
320 \& my ($class, $context) = @_;
322 \& _CONTEXT => $context,
330 \& my ($self, $context, @params) = @_;
331 \& MyClient\->new($self, @params);
336 \& sub add_to_cache { ... }
340 \& sub get_from_cache { ... }
349 \& my ($class, $server, $blah) = @_;
351 \& _SERVER => $server,
360 \& $self\->{ _SERVER }\->get_from_cache(@_);
367 \& $self\->{ _SERVER }\->add_to_cache(@_);
371 When the plugin is loaded, a \f(CW\*(C`MyServer\*(C'\fR instance is created. The \fInew()\fR
372 method is called against this object which instantiates and returns a \f(CW\*(C`MyClient\*(C'\fR
373 object, primed to communicate with the creating \f(CW\*(C`MyServer\*(C'\fR.
376 Andy Wardley <abw@wardley.org> <http://wardley.org/>
378 .IX Header "COPYRIGHT"
379 Copyright (C) 1996\-2007 Andy Wardley. All Rights Reserved.
381 This module is free software; you can redistribute it and/or
382 modify it under the same terms as Perl itself.
384 .IX Header "SEE ALSO"
385 Template, Template::Plugins, Template::Context