[Templates] how to recompiling template in each call of Template->process
Ronald J Kimball
rkimball+tt at pangeamedia.com
Thu Jun 19 14:36:16 BST 2008
Sharad Pratap wrote:
> #!/bin/env perl
> use Template;
> our $template = Template->new();
> our $txt,
> our %var;
> foreach ( qw/ a b c d e f / ) {
> $var{"x"} = $_;
> $template->process(\*DATA, \%var, \$txt);
> print "value expected ", $_, "\n";
> print $txt;
> }
> __END__
> value really comming [% x %]
Each time you call process(), TT attempts to read the template from the
DATA filehandle. However, after the first call, DATA is left pointing
at the end of the file, so the subsequent calls are getting an empty
template. You need to either seek back to the start of the template
each time, or, more simply, read in the template ahead of time:
#!/bin/env perl
use Template;
our $template = Template->new();
our $txt,
our %var;
our $t = <DATA>;
foreach ( qw/ a b c d e f / ) {
$var{"x"} = $_;
$template->process(\$t, \%var, \$txt);
print "value expected ", $_, "\n";
print $txt;
}
__END__
value really coming [% x %]
Note that process() appends the output to the variable, so you may want
to clear $txt each time through the loop.
Ronald
More information about the templates
mailing list