[Templates] Streaming template output versus waiting for entire parse

Drew Hahn dhahn at newsroomsolutions.com
Sat Dec 8 19:59:55 GMT 2007


Is there a way for the output of a template to be _streamed_ out as it is processed versus waiting for it to complete before returning?

I know I could do it with something like this below, as a result of the WRITE command in the example, but I'm looking for a way to do this without having to re-write all of my existing templates which simply write to standard out as usual.  I'm fine with a strategically-placed hack, which is what I'm working on now, but any other ideas (or a pointer to the appropriate section of code) would be helpful.  Thanks!

my %data = (
        SLEEP => sub {
                sleep(2);
        },
        WRITE => sub {
                print shift() . "\n";
        },
);

my $sample =<<EOM;
        [% x = 0 %]
        [% WHILE x < 5 %]
                [% WRITE('This is ' _ x)  %]
                [% SLEEP %]
                [% x = x + 1 %]
        [% END %]
EOM

my $template = Template->new({
        OUTPUT => \&showOutput,
});
$template->process(\$sample, \%data) || die $template->error();

sub showOutput
{
        print shift();
}


--drew

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.503 / Virus Database: 269.16.17/1177 - Release Date: 12/7/2007 1:11 PM
 



More information about the templates mailing list