Hello,

I want to support syntax like "vfield(name);" in templates but
the vfield code also needs access to the template context, which I
think excludes adding it to VARIABLES.  If I add "vfield" to BLOCKS
then it needs to be called with PROCESS.  To support the syntax
I prefer I've added a macro which calls PROCESS to the PRE_PROCESS
list.  This description isn't very clear, hopefully the code
shows what I'm doing

Is there a better way to support this simpler syntax?
The only other feature that seemed to fit the syntax and has
context access is a plugin.  I suspect there are other
techniques lurking around.

#!/usr/bin/perl
use Template;

sub vfield {
     my $context = shift;
     my $stash = $context->stash();

     my $fq_vfield = $stash->get('fq_vfield');
     my $line = $stash->get('line');

     # ... do stuff

     return "$fq_vfield @ $line";
}

$blocks{vfield} = \&vfield;  # allow [% PROCESS vfield ... %]

$blocks{vfield_macro} =      # allow [% vfield(name) %]
   '[% MACRO vfield(vf1) PROCESS vfield fq_vfield=vf1 %]'; # INCLUDE?

my $tt = Template->new(
             BLOCKS          => \%blocks,
             PRE_PROCESS     => [ 'vfield_macro' ],
         );

$tt->process(\'[% vfield("test") %]', { line => 123 } );
__END__
test @ 123

The reason I'm concerned about the syntax is that the templates
are for error messages which I prefer to be intelligible to my users
and clients in unprocessed form.

Brad

-- 
  ... But in the end, the details of a matter are important. The right
  or wrong of one's way of doing thing is found are trivial matters.
                               -- Hagakure http://bereft.net/hagakure/

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to