.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Template::Plugin::Filter 3pm" .TH Template::Plugin::Filter 3pm "2012-01-24" "perl v5.14.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Template::Plugin::Filter \- Base class for plugin filters .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& package MyOrg::Template::Plugin::MyFilter; \& \& use Template::Plugin::Filter; \& use base qw( Template::Plugin::Filter ); \& \& sub filter { \& my ($self, $text) = @_; \& \& # ...mungify $text... \& \& return $text; \& } \& \& # now load it... \& [% USE MyFilter %] \& \& # ...and use the returned object as a filter \& [% FILTER $MyFilter %] \& ... \& [% END %] .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module implements a base class for plugin filters. It hides the underlying complexity involved in creating and using filters that get defined and made available by loading a plugin. .PP To use the module, simply create your own plugin module that is inherited from the \f(CW\*(C`Template::Plugin::Filter\*(C'\fR class. .PP .Vb 1 \& package MyOrg::Template::Plugin::MyFilter; \& \& use Template::Plugin::Filter; \& use base qw( Template::Plugin::Filter ); .Ve .PP Then simply define your \f(CW\*(C`filter()\*(C'\fR method. When called, you get passed a reference to your plugin object (\f(CW$self\fR) and the text to be filtered. .PP .Vb 2 \& sub filter { \& my ($self, $text) = @_; \& \& # ...mungify $text... \& \& return $text; \& } .Ve .PP To use your custom plugin, you have to make sure that the Template Toolkit knows about your plugin namespace. .PP .Vb 3 \& my $tt2 = Template\->new({ \& PLUGIN_BASE => \*(AqMyOrg::Template::Plugin\*(Aq, \& }); .Ve .PP Or for individual plugins you can do it like this: .PP .Vb 5 \& my $tt2 = Template\->new({ \& PLUGINS => { \& MyFilter => \*(AqMyOrg::Template::Plugin::MyFilter\*(Aq, \& }, \& }); .Ve .PP Then you \f(CW\*(C`USE\*(C'\fR your plugin in the normal way. .PP .Vb 1 \& [% USE MyFilter %] .Ve .PP The object returned is stored in the variable of the same name, \&'\f(CW\*(C`MyFilter\*(C'\fR'. When you come to use it as a \f(CW\*(C`FILTER\*(C'\fR, you should add a dollar prefix. This indicates that you want to use the filter stored in the variable '\f(CW\*(C`MyFilter\*(C'\fR' rather than the filter named \&'\f(CW\*(C`MyFilter\*(C'\fR', which is an entirely different thing (see later for information on defining filters by name). .PP .Vb 3 \& [% FILTER $MyFilter %] \& ...text to be filtered... \& [% END %] .Ve .PP You can, of course, assign it to a different variable. .PP .Vb 1 \& [% USE blat = MyFilter %] \& \& [% FILTER $blat %] \& ...text to be filtered... \& [% END %] .Ve .PP Any configuration parameters passed to the plugin constructor from the \&\f(CW\*(C`USE\*(C'\fR directive are stored internally in the object for inspection by the \f(CW\*(C`filter()\*(C'\fR method (or indeed any other method). Positional arguments are stored as a reference to a list in the \f(CW\*(C`_ARGS\*(C'\fR item while named configuration parameters are stored as a reference to a hash array in the \f(CW\*(C`_CONFIG\*(C'\fR item. .PP For example, loading a plugin as shown here: .PP .Vb 1 \& [% USE blat = MyFilter \*(Aqfoo\*(Aq \*(Aqbar\*(Aq baz = \*(Aqblam\*(Aq %] .Ve .PP would allow the \f(CW\*(C`filter()\*(C'\fR method to do something like this: .PP .Vb 2 \& sub filter { \& my ($self, $text) = @_; \& \& my $args = $self\->{ _ARGS }; # [ \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq ] \& my $conf = $self\->{ _CONFIG }; # { baz => \*(Aqblam\*(Aq } \& \& # ...munge $text... \& \& return $text; \& } .Ve .PP By default, plugins derived from this module will create static filters. A static filter is created once when the plugin gets loaded via the \f(CW\*(C`USE\*(C'\fR directive and re-used for all subsequent \&\f(CW\*(C`FILTER\*(C'\fR operations. That means that any argument specified with the \f(CW\*(C`FILTER\*(C'\fR directive are ignored. .PP Dynamic filters, on the other hand, are re-created each time they are used by a \f(CW\*(C`FILTER\*(C'\fR directive. This allows them to act on any parameters passed from the \f(CW\*(C`FILTER\*(C'\fR directive and modify their behaviour accordingly. .PP There are two ways to create a dynamic filter. The first is to define a \f(CW$DYNAMIC\fR class variable set to a true value. .PP .Vb 3 \& package MyOrg::Template::Plugin::MyFilter; \& use base \*(AqTemplate::Plugin::Filter\*(Aq; \& our $DYNAMIC = 1; .Ve .PP The other way is to set the internal \f(CW\*(C`_DYNAMIC\*(C'\fR value within the \f(CW\*(C`init()\*(C'\fR method which gets called by the \f(CW\*(C`new()\*(C'\fR constructor. .PP .Vb 5 \& sub init { \& my $self = shift; \& $self\->{ _DYNAMIC } = 1; \& return $self; \& } .Ve .PP When this is set to a true value, the plugin will automatically create a dynamic filter. The outcome is that the \f(CW\*(C`filter()\*(C'\fR method will now also get passed a reference to an array of postional arguments and a reference to a hash array of named parameters. .PP So, using a plugin filter like this: .PP .Vb 1 \& [% FILTER $blat \*(Aqfoo\*(Aq \*(Aqbar\*(Aq baz = \*(Aqblam\*(Aq %] .Ve .PP would allow the \f(CW\*(C`filter()\*(C'\fR method to work like this: .PP .Vb 2 \& sub filter { \& my ($self, $text, $args, $conf) = @_; \& \& # $args = [ \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq ] \& # $conf = { baz => \*(Aqblam\*(Aq } \& } .Ve .PP In this case can pass parameters to both the \s-1USE\s0 and \s-1FILTER\s0 directives, so your \fIfilter()\fR method should probably take that into account. .PP .Vb 1 \& [% USE MyFilter \*(Aqfoo\*(Aq wiz => \*(Aqwaz\*(Aq %] \& \& [% FILTER $MyFilter \*(Aqbar\*(Aq biz => \*(Aqbaz\*(Aq %] \& ... \& [% END %] .Ve .PP You can use the \f(CW\*(C`merge_args()\*(C'\fR and \f(CW\*(C`merge_config()\*(C'\fR methods to do a quick and easy job of merging the local (e.g. \f(CW\*(C`FILTER\*(C'\fR) parameters with the internal (e.g. \f(CW\*(C`USE\*(C'\fR) values and returning new sets of conglomerated data. .PP .Vb 2 \& sub filter { \& my ($self, $text, $args, $conf) = @_; \& \& $args = $self\->merge_args($args); \& $conf = $self\->merge_config($conf); \& \& # $args = [ \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq ] \& # $conf = { wiz => \*(Aqwaz\*(Aq, biz => \*(Aqbaz\*(Aq } \& ... \& } .Ve .PP You can also have your plugin install itself as a named filter by calling the \f(CW\*(C`install_filter()\*(C'\fR method from the \f(CW\*(C`init()\*(C'\fR method. You should provide a name for the filter, something that you might like to make a configuration option. .PP .Vb 6 \& sub init { \& my $self = shift; \& my $name = $self\->{ _CONFIG }\->{ name } || \*(Aqmyfilter\*(Aq; \& $self\->install_filter($name); \& return $self; \& } .Ve .PP This allows the plugin filter to be used as follows: .PP .Vb 1 \& [% USE MyFilter %] \& \& [% FILTER myfilter %] \& ... \& [% END %] .Ve .PP or .PP .Vb 1 \& [% USE MyFilter name = \*(Aqswipe\*(Aq %] \& \& [% FILTER swipe %] \& ... \& [% END %] .Ve .PP Alternately, you can allow a filter name to be specified as the first positional argument. .PP .Vb 6 \& sub init { \& my $self = shift; \& my $name = $self\->{ _ARGS }\->[0] || \*(Aqmyfilter\*(Aq; \& $self\->install_filter($name); \& return $self; \& } \& \& [% USE MyFilter \*(Aqswipe\*(Aq %] \& \& [% FILTER swipe %] \& ... \& [% END %] .Ve .SH "EXAMPLE" .IX Header "EXAMPLE" Here's a complete example of a plugin filter module. .PP .Vb 3 \& package My::Template::Plugin::Change; \& use Template::Plugin::Filter; \& use base qw( Template::Plugin::Filter ); \& \& sub init { \& my $self = shift; \& \& $self\->{ _DYNAMIC } = 1; \& \& # first arg can specify filter name \& $self\->install_filter($self\->{ _ARGS }\->[0] || \*(Aqchange\*(Aq); \& \& return $self; \& } \& \& sub filter { \& my ($self, $text, $args, $config) = @_; \& \& $config = $self\->merge_config($config); \& my $regex = join(\*(Aq|\*(Aq, keys %$config); \& \& $text =~ s/($regex)/$config\->{ $1 }/ge; \& \& return $text; \& } \& \& 1; .Ve .SH "AUTHOR" .IX Header "AUTHOR" Andy Wardley .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (C) 1996\-2007 Andy Wardley. All Rights Reserved. .PP This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .SH "SEE ALSO" .IX Header "SEE ALSO" Template::Plugin, Template::Filters, Template::Manual::Filters