On Friday 04 January 2008, Ronald J Kimball wrote:
> I have a variable whose value I want to process as a template.  How can
> I do this?
>
> I tried [% PROCESS var %], but that looks for a block or file named var.
>
> (The value is an error message that needs some substitution performed on
> it for display.)

There are two different cases that you can use.

If the variable contains a reference to a string, then you can use PROCESS.  
I'm not sure if this was planned, but it certainly works.

perl -e 'use Template; Template::Alloy->new->process(\q{[% PROCESS $foo %]}, 
{foo => \"Hello [% 1 + 2 %]"})'

If the variable is not a string reference, but just contains the template 
itself (which is the normal case), then use the "eval" filter (which is oddly 
named and actually just processes the template).

perl -e 'use Template::Alloy; Template::Alloy->new->process(\q{[% foo | 
eval %]}, {foo => "Hello [% 1 + 2 %]"})'

Both of these examples also work in Template::Alloy, but Template::Alloy will 
cache the compiled versions based on the MD5 of the passed string (plus it 
parses more quickly) so there isn't as much of a hit to using "eval".

Paul

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

Reply via email to