[Templates] Templates : Pass perl variables to BLOCKS
Andy Wardley
abw at wardley.org
Tue Oct 16 17:38:54 BST 2007
Madan kumar nath wrote:
> [% PERL %]
> my @arr = ("hello", "madan", "kumar");
> $context->stash->set('funArgValue',\@arr);
> my $yy = $stash->get('funArgValue');
> my @yy = @$yy;
> print "\nValue in main : @yy\n";
>
> [%# Function Call %]
> [% PROCESS test funArg=funArgValue %]
> [% END %]
The template content of a [% PERL %]...[% END %] block is processed
*before* it is evaluated as Perl. So the PROCESS is being done first,
and the output it generates is then evaluated as part of the PERL
expression.
Here's a simple example to show what I mean.
[% name = 'World' %]
[% PERL %]
print "Hello [% name %]";
[% END %]
This is the same thing as:
[% PERL %]
print "Hello World";
[% END %]
Which ultimately becomes (in Perl)
eval {
print "Hello World";
};
As Mihai points out in his reply, you should be doing this instead of
the PROCESS directive.
$context->process('test', { funArg => \@arr });
This performs the equivalent of the [% PROCESS test funArg=funArgValue %]
directive, and it will happen at the right time, i.e. in the PERL block,
after funArgValue has been set.
Alternately, if it's just defining Perl data that you want to do then
you can do most things direct from TT without needing a PERL block at all.
e.g.
[% funArgValue=["hello", "madan", 'kumar"] %]
[% PROCESS test funArg=funArgValue %]
Or even:
[% PROCESS test funArg=["hello", "madan", 'kumar"] %]
If you really do need a PERL block, and you also want to use the PROCESS
directive, then do this instead:
[% PERL %]
my @arr = ("hello", "madan", "kumar");
$context->stash->set('funArgValue',\@arr);
[% END %]
[% PROCESS test funArg=funArgValue %]
HTH
A
More information about the templates
mailing list