[Templates] custom filters and chaining

Andy Wardley abw@wardley.org
Wed, 24 May 2006 08:08:17 +0100


Dustin Frazier wrote:
> I have two questions regarding the use of filters.  I use the Template 
> Toolkit via ttree rather than writing custom Perl code that uses the 
> underlying modules.  First question: Is it possible to define and use 
> custom filters via ttree?

No, not directly from ttree.  But you can define filters using a plugin.

    package Template::Plugin::MyFilters;
    use base 'Template::Plugin';

    sub new {
      my ($class, $context) = @_;
      $context->define_filter( name => sub { ... } );
    }

Then put a [% USE MyFilters %] directive at the top of a PRE_PROCESS 
template.  The plugin will be loaded and the filters defined.

> [% FILTER html_entity | wrap(95) | indent("\t") %]

That doesn't work I'm afraid, and it's too hard to fix it in the current 
parser.

It is planned for TT3, although '+' will be the delimiting character (as 
  per PROCESS, INCLUDE etc which currently accept this form)

    [% FILTER html_entity + wrap(95) + indent("\t") %]
       ...
    [% END %]


Cheers
A