[Templates] "sub(x)" syntax with $context access

Andy Wardley abw at wardley.org
Tue Nov 13 08:25:04 GMT 2007


Brad Bowman wrote:
> the vfield code also needs access to the template context
[...]
 > The only other feature that seemed to fit the syntax and has
 > context access is a plugin.

Hi Brad,

Yes, you probably want to code a plugin for this.  When you USE the plugin
you get a reference to the context.  You can either return an object which
saves the context internally, or a closure like this:

   package Brads::Plugin::vfield;
   use base 'Template::Plugin';

   sub new {
       my ($class, $context) = @_;
       sub {
           my $stash = $context->stash();
           my $fq_vfield = $stash->get('fq_vfield');
           my $line = $stash->get('line');
           # ... do stuff
           return "$fq_vfield @ $line";
       }
   }

Then:

   [% USE vfield %]       # you can do this in a PRE_PROCESS template/block
   [% vfield('test') %]


HTH
A




More information about the templates mailing list