<?php
define("ABS_PATH", $_SERVER['DOCUMENT_ROOT']);
include(ABS_PATH . "/head_nav.php");
include(ABS_PATH . "/middle.php");
?>
          <div class="section">
            <div class="toc-wrapper">
              <span class="toc-title">Contents:</span>
              <ul class="toc">
                  <li class=""><a href="#SYNOPSIS">SYNOPSIS</a></li>
                  <li class=""><a href="#DESCRIPTION">DESCRIPTION</a></li>
                  <li class=""><a href="#EXAMPLE">EXAMPLE</a></li>
                  <li class=""><a href="#AUTHOR">AUTHOR</a></li>
                  <li class=""><a href="#COPYRIGHT">COPYRIGHT</a></li>
                  <li class=""><a href="#SEE_ALSO">SEE ALSO</a></li>
              </ul>
            </div>
          </div>
          
                <div class="pod">
            <div class="section">
              <div class="head">
                <h1 id="SYNOPSIS">SYNOPSIS</h1>
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
              </div>
              <div class="body">
                <pre>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...
<span class="tt">[% USE MyFilter %]</span>

# ...and use the returned object as a filter
<span class="tt">[% FILTER $MyFilter %]</span>
  ...
<span class="tt">[% END %]</span></pre>
              </div>
            </div>
            <div class="section">
              <div class="head">
                <h1 id="DESCRIPTION">DESCRIPTION</h1>
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
              </div>
              <div class="body">
                <p>
                      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.
                    </p>
                    <p>
                      To use the module, simply create your own plugin module that is inherited
                      from the <code>Template::Plugin::Filter</code> class.
                    </p>
                    <pre>package MyOrg::Template::Plugin::MyFilter;

use Template::Plugin::Filter;
use base qw( Template::Plugin::Filter );</pre>
                    <p>
                      Then simply define your <code>filter()</code> method. When called, you
                      get passed a reference to your plugin object (<code>$self</code>) and the
                      text to be filtered.
                    </p>
                    <pre>sub filter {
    my ($self, $text) = @_;

    # ...mungify $text...

    return $text;
}</pre>
                    <p>
                      To use your custom plugin, you have to make sure that the Template
                      Toolkit knows about your plugin namespace.
                    </p>
                    <pre>my $tt2 = Template-&gt;new({
    PLUGIN_BASE =&gt; 'MyOrg::Template::Plugin',
});</pre>
                    <p>
                      Or for individual plugins you can do it like this:
                    </p>
                    <pre>my $tt2 = Template-&gt;new({
    PLUGINS =&gt; {
        MyFilter =&gt; 'MyOrg::Template::Plugin::MyFilter',
    },
});</pre>
                    <p>
                      Then you <code>USE</code> your plugin in the normal way.
                    </p>
                    <pre><span class="tt">[% USE MyFilter %]</span></pre>
                    <p>
                      The object returned is stored in the variable of the same name,
                      '<code>MyFilter</code>'. When you come to use it as a
                      <code>FILTER</code>, you should add a dollar prefix. This indicates that
                      you want to use the filter stored in the variable '<code>MyFilter</code>'
                      rather than the filter named '<code>MyFilter</code>', which is an
                      entirely different thing (see later for information on defining filters
                      by name).
                    </p>
                    <pre><span class="tt">[% FILTER $MyFilter %]</span>
   ...text to be filtered...
<span class="tt">[% END %]</span></pre>
                    <p>
                      You can, of course, assign it to a different variable.
                    </p>
                    <pre><span class="tt">[% USE blat = MyFilter %]</span>

<span class="tt">[% FILTER $blat %]</span>
   ...text to be filtered...
<span class="tt">[% END %]</span></pre>
                    <p>
                      Any configuration parameters passed to the plugin constructor from the
                      <code>USE</code> directive are stored internally in the object for
                      inspection by the <code>filter()</code> method (or indeed any other
                      method). Positional arguments are stored as a reference to a list in the
                      <code>_ARGS</code> item while named configuration parameters are stored
                      as a reference to a hash array in the <code>_CONFIG</code> item.
                    </p>
                    <p>
                      For example, loading a plugin as shown here:
                    </p>
                    <pre><span class="tt">[% USE blat = MyFilter 'foo' 'bar' baz = 'blam' %]</span></pre>
                    <p>
                      would allow the <code>filter()</code> method to do something like this:
                    </p>
                    <pre>sub filter {
    my ($self, $text) = @_;

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

    # ...munge $text...

    return $text;
}</pre>
                    <p>
                      By default, plugins derived from this module will create static filters.
                      A static filter is created once when the plugin gets loaded via the
                      <code>USE</code> directive and re-used for all subsequent
                      <code>FILTER</code> operations. That means that any argument specified
                      with the <code>FILTER</code> directive are ignored.
                    </p>
                    <p>
                      Dynamic filters, on the other hand, are re-created each time they are
                      used by a <code>FILTER</code> directive. This allows them to act on any
                      parameters passed from the <code>FILTER</code> directive and modify their
                      behaviour accordingly.
                    </p>
                    <p>
                      There are two ways to create a dynamic filter. The first is to define a
                      <code>$DYNAMIC</code> class variable set to a true value.
                    </p>
                    <pre>package MyOrg::Template::Plugin::MyFilter;
use base 'Template::Plugin::Filter';
our $DYNAMIC = 1;</pre>
                    <p>
                      The other way is to set the internal <code>_DYNAMIC</code> value within
                      the <code>init()</code> method which gets called by the
                      <code>new()</code> constructor.
                    </p>
                    <pre>sub init {
    my $self = shift;
    $self-&gt;{ _DYNAMIC } = 1;
    return $self;
}</pre>
                    <p>
                      When this is set to a true value, the plugin will automatically create a
                      dynamic filter. The outcome is that the <code>filter()</code> method will
                      now also get passed a reference to an array of postional arguments and a
                      reference to a hash array of named parameters.
                    </p>
                    <p>
                      So, using a plugin filter like this:
                    </p>
                    <pre><span class="tt">[% FILTER $blat 'foo' 'bar' baz = 'blam' %]</span></pre>
                    <p>
                      would allow the <code>filter()</code> method to work like this:
                    </p>
                    <pre>sub filter {
    my ($self, $text, $args, $conf) = @_;

    # $args = [ 'foo', 'bar' ]
    # $conf = { baz =&gt; 'blam' }
}</pre>
                    <p>
                      In this case can pass parameters to both the USE and FILTER directives,
                      so your filter() method should probably take that into account.
                    </p>
                    <pre><span class="tt">[% USE MyFilter 'foo' wiz =&gt; 'waz' %]</span>

<span class="tt">[% FILTER $MyFilter 'bar' biz =&gt; 'baz' %]</span>
   ...
<span class="tt">[% END %]</span></pre>
                    <p>
                      You can use the <code>merge_args()</code> and <code>merge_config()</code>
                      methods to do a quick and easy job of merging the local (e.g.
                      <code>FILTER</code>) parameters with the internal (e.g. <code>USE</code>)
                      values and returning new sets of conglomerated data.
                    </p>
                    <pre>sub filter {
    my ($self, $text, $args, $conf) = @_;

    $args = $self-&gt;merge_args($args); 
    $conf = $self-&gt;merge_config($conf);

    # $args = [ 'foo', 'bar' ]      
    # $conf = { wiz =&gt; 'waz', biz =&gt; 'baz' }        
    ...
}</pre>
                    <p>
                      You can also have your plugin install itself as a named filter by calling
                      the <code>install_filter()</code> method from the <code>init()</code>
                      method. You should provide a name for the filter, something that you
                      might like to make a configuration option.
                    </p>
                    <pre>sub init {
    my $self = shift;
    my $name = $self-&gt;{ _CONFIG }-&gt;{ name } || 'myfilter';
    $self-&gt;install_filter($name);
    return $self;
}</pre>
                    <p>
                      This allows the plugin filter to be used as follows:
                    </p>
                    <pre><span class="tt">[% USE MyFilter %]</span>

<span class="tt">[% FILTER myfilter %]</span> 
   ... 
<span class="tt">[% END %]</span></pre>
                    <p>
                      or
                    </p>
                    <pre><span class="tt">[% USE MyFilter name = 'swipe' %]</span>

<span class="tt">[% FILTER swipe %]</span> 
   ... 
<span class="tt">[% END %]</span></pre>
                    <p>
                      Alternately, you can allow a filter name to be specified as the first
                      positional argument.
                    </p>
                    <pre>sub init {
    my $self = shift;
    my $name = $self-&gt;{ _ARGS }-&gt;[0] || 'myfilter';
    $self-&gt;install_filter($name);
    return $self;
}

<span class="tt">[% USE MyFilter 'swipe' %]</span>

<span class="tt">[% FILTER swipe %]</span>
   ...
<span class="tt">[% END %]</span></pre>
              </div>
            </div>
            <div class="section">
              <div class="head">
                <h1 id="EXAMPLE">EXAMPLE</h1>
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
              </div>
              <div class="body">
                <p>
                      Here's a complete example of a plugin filter module.
                    </p>
                    <pre>package My::Template::Plugin::Change;
use Template::Plugin::Filter;
use base qw( Template::Plugin::Filter );

sub init {
    my $self = shift;

    $self-&gt;{ _DYNAMIC } = 1;

    # first arg can specify filter name
    $self-&gt;install_filter($self-&gt;{ _ARGS }-&gt;[0] || 'change');

    return $self;
}

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

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

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

    return $text;
}

1;</pre>
              </div>
            </div>
            <div class="section">
              <div class="head">
                <h1 id="AUTHOR">AUTHOR</h1>
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
              </div>
              <div class="body">
                <p>
                      Andy Wardley &lt;abw@wardley.org&gt; <a
                      href="https://wardley.org/">https://wardley.org/</a>
                    </p>
              </div>
            </div>
            <div class="section">
              <div class="head">
                <h1 id="COPYRIGHT">COPYRIGHT</h1>
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
              </div>
              <div class="body">
                <p>
                      Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
                    </p>
                    <p>
                      This module is free software; you can redistribute it and/or modify it
                      under the same terms as Perl itself.
                    </p>
              </div>
            </div>
            <div class="section">
              <div class="head">
                <h1 id="SEE_ALSO">SEE ALSO</h1>
                <a href="#body" class="top" title="Back up to the top of the page" >Top</a>
              </div>
              <div class="body">
                <p>
                      <a href="../../../modules/Template/Plugin.php">Template::Plugin</a>, <a
                      href="../../../modules/Template/Filters.php">Template::Filters</a>, <a
                      href="../../../manual/Filters.php">Template::Manual::Filters</a>
                    </p>
              </div>
            </div>
            
            </div>
<?php include(ABS_PATH . "/footer.php"); ?>
