%PDF- %PDF-
| Direktori : /proc/self/root/proc/thread-self/root/usr/local/share/man/man3/ |
| Current File : //proc/self/root/proc/thread-self/root/usr/local/share/man/man3/Template::Plugin::Filter.3pm |
.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)
.\"
.\" 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" ''
. ds C`
. ds C'
'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 >0, 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.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Template::Plugin::Filter 3"
.TH Template::Plugin::Filter 3 "2024-06-21" "perl v5.32.1" "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 positional
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 \fBfilter()\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 <abw@wardley.org> <http://wardley.org/>
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
Copyright (C) 1996\-2022 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