[Templates] Streaming template output versus waiting for entire parse

Drew Hahn dhahn at newsroomsolutions.com
Mon Dec 10 13:48:17 GMT 2007


Kelly,
 
Actually, the output is not a webpage.  Imagine for a second that the output is an LED sign and, say, I'm trying to output a countdown clock that goes "10", "9", "8", etc. and sleeps one second between each second.  What I don't want is 10 seconds of nothing, then suddenly $output being "10 9 8 7 6 5 4 3 2 1 0".  I want to receive the "10", then it will wait 1 second, then send me the "9", then wait one second, etc.
 
I could do that with a "WRITE" command where anything destined to go to the LED sign is sent through that, which would work in real-time, but my question is whether that could be done by overriding the normal standard output method so I wouldn't have to re-do thousands of templates.
 
--drew

   _____  

From: Kelly Thompson [mailto:kalexthompson at gmail.com] 
Sent: Saturday, December 08, 2007 8:44 PM
To: Drew Hahn
Subject: Re: [Templates] Streaming template output versus waiting for entire parse


Look into Template Toolkit and Mod_perl....

That's way quicker!

And, Andy made it readily available.





On Dec 8, 2007 2:59 PM, Drew Hahn <HYPERLINK "mailto:dhahn at newsroomsolutions.com" dhahn at newsroomsolutions.com> wrote:


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


_______________________________________________
templates mailing list
HYPERLINK "mailto:templates at template-toolkit.org"templates at template-toolkit.org
HYPERLINK "http://mail.template-toolkit.org/mailman/listinfo/templates" \nhttp://mail.template-toolkit.org/mailman/listinfo/templates




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



No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.503 / Virus Database: 269.16.17/1178 - Release Date: 12/8/2007 11:59 AM
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.template-toolkit.org/pipermail/templates/attachments/20071210/0c623357/attachment.htm 


More information about the templates mailing list