[Templates] "sub(x)" syntax with $context access
Paul Seamons
mail at seamons.com
Tue Nov 13 14:55:24 GMT 2007
Yet another way to do it - but you would have to enable EVAL_PERL which has
plenty of drawbacks (if you used Template::Alloy you could limit the scope of
EVAL_PERL to a smaller area).
In your template put something like the following:
[% PERL %]
require Scalar::Util;
my $copy = $context;
my $real = $stash->get('real_vfield_sub');
my $proxy = sub { $real->($copy, @_) };
$stash->set('vfield', $proxy);
Scalar::Util::weaken($copy);
[% END %]
[% vfield('test') %]
You would then load the template like so:
my $tt = Template->new(
EVAL_PERL => 1,
VARIABLES => {
real_vfield_sub => sub {
my ($context, $txt) = @_;
return "(($context: $txt))\n";
},
},
);
$tt->process('my_template.html');
There are many variations - but this allows you to do more from within your
template. Ultimately the USE plugin option is the better option to go with.
Paul
More information about the templates
mailing list