[%# # IMPORTANT NOTE # This documentation is generated automatically from source # templates. Any changes you make here may be lost. # # The 'docsrc' documentation source bundle is available for download # from http://www.template-toolkit.org/docs.html and contains all # the source templates, XML files, scripts, etc., from which the # documentation for the Template Toolkit is built. -%] [% META book = 'Modules' page = 'Plugin_Filter' %] [% WRAPPER toc; PROCESS tocitem title ="SYNOPSIS" subs = []; PROCESS tocitem title ="DESCRIPTION" subs = []; PROCESS tocitem title ="EXAMPLE" subs = []; PROCESS tocitem title ="AUTHOR" subs = []; PROCESS tocitem title ="VERSION" subs = []; PROCESS tocitem title ="COPYRIGHT" subs = []; PROCESS tocitem title ="SEE ALSO" subs = []; END %] [% WRAPPER section title="SYNOPSIS" -%]

        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...
        [% tt_start_tag %] USE MyFilter [% tt_end_tag %]

        # ...and use the returned object as a filter
        [% tt_start_tag %] FILTER $MyFilter [% tt_end_tag %]
          ...
        [% tt_start_tag %] END [% tt_end_tag %]

[%- END %] [% WRAPPER section title="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.

To use the module, simply create your own plugin module that is inherited from the Template::Plugin::Filter class.

        package MyOrg::Template::Plugin::MyFilter;

        use Template::Plugin::Filter;
        use base qw( Template::Plugin::Filter );

Then simply define your filter() method. When called, you get passed a reference to your plugin object ($self) and the text to be filtered.

        sub filter {
    	my ($self, $text) = @_;

    	# ...mungify $text...

    	return $text;
        }

To use your custom plugin, you have to make sure that the Template Toolkit knows about your plugin namespace.

        my $tt2 = Template->new({
    	PLUGIN_BASE => 'MyOrg::Template::Plugin',
        });

Or for individual plugins you can do it like this:

        my $tt2 = Template->new({
    	PLUGINS => {
    	    MyFilter => 'MyOrg::Template::Plugin::MyFilter',
    	},
        });

Then you USE your plugin in the normal way.

        [% tt_start_tag %] USE MyFilter [% tt_end_tag %]

The object returned is stored in the variable of the same name, 'MyFilter'. When you come to use it as a FILTER, you should add a dollar prefix. This indicates that you want to use the filter stored in the variable 'MyFilter' rather than the filter named 'MyFilter', which is an entirely different thing (see later for information on defining filters by name).

        [% tt_start_tag %] FILTER $MyFilter [% tt_end_tag %]
           ...text to be filtered...
        [% tt_start_tag %] END [% tt_end_tag %]

You can, of course, assign it to a different variable.

        [% tt_start_tag %] USE blat = MyFilter [% tt_end_tag %]

        [% tt_start_tag %] FILTER $blat [% tt_end_tag %]
           ...text to be filtered...
        [% tt_start_tag %] END [% tt_end_tag %]

Any configuration parameters passed to the plugin constructor from the USE directive are stored internally in the object for inspection by the filter() method (or indeed any other method). Positional arguments are stored as a reference to a list in the _ARGS item while named configuration parameters are stored as a reference to a hash array in the _CONFIG item.

For example, loading a plugin as shown here:

        [% tt_start_tag %] USE blat = MyFilter 'foo' 'bar' baz = 'blam' [% tt_end_tag %]

would allow the filter() method to do something like this:

        sub filter {
    	my ($self, $text) = @_;

    	my $args = $self->{ _ARGS   };  # [ 'foo', 'bar' ]
    	my $conf = $self->{ _CONFIG };  # { baz => 'blam' }

    	# ...munge $text...

    	return $text;
        }

By default, plugins derived from this module will create static filters. A static filter is created once when the plugin gets loaded via the USE directive and re-used for all subsequent FILTER operations. That means that any argument specified with the FILTER directive are ignored.

Dynamic filters, on the other hand, are re-created each time they are used by a FILTER directive. This allows them to act on any parameters passed from the FILTER directive and modify their behaviour accordingly.

There are two ways to create a dynamic filter. The first is to define a $DYNAMIC class variable set to a true value.

        package MyOrg::Template::Plugin::MyFilter;

        use Template::Plugin::Filter;
        use base qw( Template::Plugin::Filter );
        use vars qw( $DYNAMIC );

        $DYNAMIC = 1;

The other way is to set the internal _DYNAMIC value within the init() method which gets called by the new() constructor.

        sub init {
    	my $self = shift;
    	$self->{ _DYNAMIC } = 1;
    	return $self;
        }

When this is set to a true value, the plugin will automatically create a dynamic filter. The outcome is that the filter() method will now also get passed a reference to an array of postional arguments and a reference to a hash array of named parameters.

So, using a plugin filter like this:

        [% tt_start_tag %] FILTER $blat 'foo' 'bar' baz = 'blam' [% tt_end_tag %]

would allow the filter() method to work like this:

        sub filter {
    	my ($self, $text, $args, $conf) = @_;

    	# $args = [ 'foo', 'bar' ]
    	# $conf = { baz => 'blam' }

        }

In this case can pass parameters to both the USE and FILTER directives, so your filter() method should probably take that into account.

        [% tt_start_tag %] USE MyFilter 'foo' wiz => 'waz' [% tt_end_tag %]

        [% tt_start_tag %] FILTER $MyFilter 'bar' biz => 'baz' [% tt_end_tag %]
           ...
        [% tt_start_tag %] END [% tt_end_tag %]

You can use the merge_args() and merge_config() methods to do a quick and easy job of merging the local (e.g. FILTER) parameters with the internal (e.g. USE) values and returning new sets of conglomerated data.

        sub filter {
    	my ($self, $text, $args, $conf) = @_;

    	$args = $self->merge_args($args);
    	$conf = $self->merge_config($conf);

    	# $args = [ 'foo', 'bar' ]
    	# $conf = { wiz => 'waz', biz => 'baz' }
    	...
        }

You can also have your plugin install itself as a named filter by calling the install_filter() method from the init() method. You should provide a name for the filter, something that you might like to make a configuration option.

        sub init {
    	my $self = shift;
    	my $name = $self->{ _CONFIG }->{ name } || 'myfilter';
    	$self->install_filter($name);
    	return $self;
        }

This allows the plugin filter to be used as follows:

        [% tt_start_tag %] USE MyFilter [% tt_end_tag %]

        [% tt_start_tag %] FILTER myfilter [% tt_end_tag %]
           ...
        [% tt_start_tag %] END [% tt_end_tag %]

or

        [% tt_start_tag %] USE MyFilter name = 'swipe' [% tt_end_tag %]

        [% tt_start_tag %] FILTER swipe [% tt_end_tag %]
           ...
        [% tt_start_tag %] END [% tt_end_tag %]

Alternately, you can allow a filter name to be specified as the first positional argument.

        sub init {
    	my $self = shift;
    	my $name = $self->{ _ARGS }->[0] || 'myfilter';
    	$self->install_filter($name);
    	return $self;
        }

        [% tt_start_tag %] USE MyFilter 'swipe' [% tt_end_tag %]

        [% tt_start_tag %] FILTER swipe [% tt_end_tag %]
           ...
        [% tt_start_tag %] END [% tt_end_tag %]

[%- END %] [% WRAPPER section title="EXAMPLE" -%]

Here's a complete example of a plugin filter module.

        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] || 'change');

    	return $self;
        }

        sub filter {
    	my ($self, $text, $args, $config) = @_;

    	$config = $self->merge_config($config);
    	my $regex = join('|', keys %$config);

    	$text =~ s/($regex)/$config->{ $1 }/ge;

    	return $text;
        }

        1;

[%- END %] [% WRAPPER section title="AUTHOR" -%]

Andy Wardley <abw@wardley.org>

[% ttlink('http://wardley.org/', 'http://wardley.org/') -%]

[%- END %] [% WRAPPER section title="VERSION" -%]

1.33, distributed as part of the Template Toolkit version 2.15, released on 26 May 2006.

[%- END %] [% WRAPPER section title="COPYRIGHT" -%]

      Copyright (C) 1996-2006 Andy Wardley.  All Rights Reserved.
      Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

[%- END %] [% WRAPPER section title="SEE ALSO" -%]

[% ttlink('Template::Plugin', 'Template::Plugin') -%], [% ttlink('Template::Filters', 'Template::Filters') -%], [% ttlink('Template::Manual::Filters', 'Template::Manual::Filters') -%]

[%- END %]