John Smith wrote:
I want to be able to include a template component in a template where that component is not a file. I'm sure its somewhere in the man pages, but I can't find it. This is what I tried: perl code:
use strict;
use Template;
my $block = <<BLOCK;
----------------------------------------------
Hello, [% name %]
----------------------------------------------
BLOCK
my $tt = Template->new() or die;
my $ttData =
{
  blockdata => $block,
  name => 'John'
};
$tt->process("testblock.tt", $ttData) or die;
testblock.tt:
[% MACRO ablock BLOCK %]
[% blockdata %]
[% END %]
[% INCLUDE ablock %]
Doesn't work. Any ideas?
John Smith



Hi John,

I probably shouldn't post on this as I haven't tried what you mentioned. However, have you tried 
adding an "| eval" to the blockdata variable retrieve?  My guess is that you are just 
getting the non-processed block you specified in the perl code above. (Let us know what you mean by 
"doesn't work" if it's not it.) :)

testblock.tt:
[% MACRO ablock BLOCK %]
[% blockdata | eval %]
[% END %]
[% INCLUDE ablock %]


As an alternative you might try the following, which I have also not tested, 
and am not sure will even work, but am just providing as something you can try. 
I've never tried two procesess, in a row, and am not sure whether they will 
have the same context or not, but if it worked, it'd be a simpler solution.

=================================================
perl code:
use strict;
use Template;
my $block = <<BLOCK;
[% BLOCK ablock %]
----------------------------------------------
Hello, [% name %]
----------------------------------------------
[% END %]
BLOCK
my $tt = Template->new() or die;
my $ttData =
{
 blockdata => $block,
 name => 'John'
};
my $result;
$tt->process(\$block, $ttData, \$result) or die;
$tt->process("testblock.tt", $ttData) or die;

testblock.tt:
[% INCLUDE ablock %]
=================================================


-- Josh

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

Reply via email to