Brad Bowman wrote:
> 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.


Hi Brad,

I'm not sure I understand your problem entirely. Here is some rough code that 
you may be able to get to do what you want, though. Basically it relies on 
having the $tt object available to your routine and passing the vfield routine 
in the process vars.

my $tt;

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

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

     # ... do stuff

     return "$fq_vfield @ $line";
}

$tt = Template->new();
$tt->process(\'[% vfield("test") %]', { line => 123,
                                        test => 5
                                        vfield => \&vfield } );

# grabs whatever is in the variable named "test". So output will be:
5 @ 123

-- Josh

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

Reply via email to