[Templates] getting a string [% variable %]'s contents PROCESS/INCLUDE'd like a file's contents

Lee.M jupiter at jupiterhost.net
Thu Jul 3 01:09:44 BST 2008


Hello all,

Thanks in advance for any feedback :)

I can't seem to find out if/how this is possible but:

If a variable passed to process() is a string that also includes TT  
directives, is there a way to have the variable's contents processed?  
Similar to PROCESS or INCLUDE but with a variable's contents

For example, how do we get this:

my $stp = "A: [% a %]\nB: [% b %]\nC: [% c %]";
$tt->process(\$stp, { a=>"AAA", b=>"BBB", c=>'C is A: [% a %]'}, \ 
$buf) || die $tt->error();

To render

A: AAA
B: BBB
C: C is A: AAA

instead of:

A: AAA
B: BBB
C: C is A: [% a %]

The only trick so far is to process() it twice, which seem excessive  
since we have to reprocess all the output and not just pieces of text  
within it:

#!/usr/bin/perl

use Template;

my $tt = Template->new();

# how to get 'c' processed, as in: C is A: AAA

my $stp = "A: [% a %]\nB: [% b %]\nC: [% c %]";
my $buf;
$tt->process(\$stp, { a=>"AAA", b=>"BBB", c=>'C is A: [% a %]'}, \ 
$buf) || die $tt->error();
$tt->process(\$buf, { a=>"AAA", b=>"BBB", c=>'C is A: [% a %]'}) ||  
die $tt->error();
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.template-toolkit.org/pipermail/templates/attachments/20080702/383433c0/attachment.htm 


More information about the templates mailing list