[Templates] adding support for incremental output
Myk Melez
myk@mozilla.org
Thu, 07 Mar 2002 18:52:44 +0100
This is a multi-part message in MIME format.
--------------010101090402050104070103
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
This version of the patch fixes a problem with additional arguments to
the PROCESS directive. Still looking for feedback.
-myk
--------------010101090402050104070103
Content-Type: text/plain;
name="patch-flushv2"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="patch-flushv2"
diff -Naur Template-Toolkit-2.06d/lib/Template/Context.pm Template-Toolkit-2.06d-flush/lib/Template/Context.pm
--- Template-Toolkit-2.06d/lib/Template/Context.pm Tue Jan 22 19:09:32 2002
+++ Template-Toolkit-2.06d-flush/lib/Template/Context.pm Thu Mar 7 18:41:30 2002
@@ -258,7 +258,7 @@
#------------------------------------------------------------------------
sub process {
- my ($self, $template, $params) = @_;
+ my ($self, $template, $outstream, $params) = @_;
my ($trim, $blocks) = @$self{ qw( TRIM BLOCKS ) };
my (@compiled, $name, $compiled);
my ($stash, $tblocks, $error, $tmpout);
@@ -267,6 +267,9 @@
# my ($blocks, $output);
# my $name = $template;
+ $self->{ OUTSTREAM } = $outstream
+ if UNIVERSAL::isa($template, 'Template::Document');
+
$template = [ $template ] unless ref $template eq 'ARRAY';
# fetch compiled template for each name specified
@@ -679,6 +682,18 @@
}
+#------------------------------------------------------------------------
+# output()
+#
+# Outputs generated content to the output stream.
+#------------------------------------------------------------------------
+
+sub output {
+ my ($self, $output) = @_;
+ return Template::output($self->{ OUTSTREAM }, $output);
+}
+
+
#------------------------------------------------------------------------
# AUTOLOAD
#
diff -Naur Template-Toolkit-2.06d/lib/Template/Directive.pm Template-Toolkit-2.06d-flush/lib/Template/Directive.pm
--- Template-Toolkit-2.06d/lib/Template/Directive.pm Tue Nov 20 12:29:28 2001
+++ Template-Toolkit-2.06d-flush/lib/Template/Directive.pm Thu Mar 7 18:43:13 2002
@@ -339,6 +339,7 @@
my ($file, $args) = @$nameargs;
my $hash = shift @$args;
$file = $class->filenames($file);
+ $file .= ', $context->{ OUTSTREAM }';
$file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
return "$OUTPUT \$context->process($file);";
}
@@ -694,6 +695,16 @@
}
#------------------------------------------------------------------------
+# flush() [% FLUSH %]
+#
+# NOTE: this is redundant, being hard-coded (for now) into Parser.yp
+#------------------------------------------------------------------------
+
+sub flush {
+ return "\$context->output(\$output); \$output = '';";
+}
+
+#------------------------------------------------------------------------
# break() [% BREAK %]
#
# NOTE: this is redundant, being hard-coded (for now) into Parser.yp
diff -Naur Template-Toolkit-2.06d/lib/Template/Service.pm Template-Toolkit-2.06d-flush/lib/Template/Service.pm
--- Template-Toolkit-2.06d/lib/Template/Service.pm Tue Jan 22 19:09:32 2002
+++ Template-Toolkit-2.06d-flush/lib/Template/Service.pm Thu Mar 7 18:41:30 2002
@@ -54,7 +54,7 @@
#------------------------------------------------------------------------
sub process {
- my ($self, $template, $params) = @_;
+ my ($self, $template, $outstream, $params) = @_;
my $context = $self->{ CONTEXT };
my ($name, $output, $procout, $error);
$output = '';
@@ -79,7 +79,7 @@
# PRE_PROCESS
eval {
foreach $name (@{ $self->{ PRE_PROCESS } }) {
- $output .= $context->process($name);
+ $output .= $context->process($name, $outstream);
}
};
last SERVICE if ($error = $@);
@@ -87,7 +87,7 @@
# PROCESS
eval {
foreach $name (@{ $self->{ PROCESS } || [ $template ] }) {
- $procout .= $context->process($name);
+ $procout .= $context->process($name, $outstream);
}
};
if ($error = $@) {
@@ -99,7 +99,7 @@
# POST_PROCESS
eval {
foreach $name (@{ $self->{ POST_PROCESS } }) {
- $output .= $context->process($name);
+ $output .= $context->process($name, $outstream);
}
};
last SERVICE if ($error = $@);
diff -Naur Template-Toolkit-2.06d/lib/Template.pm Template-Toolkit-2.06d-flush/lib/Template.pm
--- Template-Toolkit-2.06d/lib/Template.pm Tue Jan 22 19:09:32 2002
+++ Template-Toolkit-2.06d-flush/lib/Template.pm Thu Mar 7 18:41:30 2002
@@ -47,29 +47,25 @@
#------------------------------------------------------------------------
# process($input, \%replace, $output)
#
-# Main entry point for the Template Toolkit. The Template module
+# Main entry point for the Template Toolkit. The Template module
# delegates most of the processing effort to the underlying SERVICE
-# object, an instance of the Template::Service class.
+# object, an instance of the Template::Service class.
#------------------------------------------------------------------------
sub process {
my ($self, $template, $vars, $outstream) = @_;
my ($output, $error);
- $output = $self->{ SERVICE }->process($template, $vars);
-
- if (defined $output) {
- $outstream ||= $self->{ OUTPUT };
- unless (ref $outstream) {
- my $outpath = $self->{ OUTPUT_PATH };
- $outstream = "$outpath/$outstream" if $outpath;
- }
+ $outstream ||= $self->{ OUTPUT };
+ unless (ref $outstream) {
+ my $outpath = $self->{ OUTPUT_PATH };
+ $outstream = "$outpath/$outstream" if $outpath;
+ }
- # send processed template to output stream, checking for error
- return ($self->error($error))
- if ($error = &_output($outstream, $output));
+ $output = $self->{ SERVICE }->process($template, $outstream, $vars);
- return 1;
+ if (defined $output) {
+ return &output($outstream, $output);
}
else {
return $self->error($self->{ SERVICE }->error);
@@ -77,6 +73,26 @@
}
+#------------------------------------------------------------------------
+# output()
+#
+# Outputs generated content to the output stream.
+#------------------------------------------------------------------------
+
+sub output {
+ my ($outstream, $output) = @_;
+ my ($error);
+
+ if (defined $output) {
+ # send processed template to output stream, checking for error
+ return (Template::Base::error($error))
+ if ($error = &_output($outstream, $output));
+ }
+
+ return 1;
+}
+
+
#------------------------------------------------------------------------
# service()
#
diff -Naur Template-Toolkit-2.06d/parser/Grammar.pm.skel Template-Toolkit-2.06d-flush/parser/Grammar.pm.skel
--- Template-Toolkit-2.06d/parser/Grammar.pm.skel Thu Nov 29 17:44:36 2001
+++ Template-Toolkit-2.06d-flush/parser/Grammar.pm.skel Thu Mar 7 18:41:30 2002
@@ -52,7 +52,7 @@
GET CALL SET DEFAULT INSERT INCLUDE PROCESS WRAPPER BLOCK END
USE PLUGIN FILTER MACRO PERL RAWPERL TO STEP AND OR NOT DIV MOD
IF UNLESS ELSE ELSIF FOR NEXT WHILE SWITCH CASE META
- TRY THROW CATCH FINAL LAST RETURN STOP CLEAR VIEW
+ TRY THROW CATCH FINAL LAST RETURN STOP CLEAR VIEW FLUSH
);
# for historical reasons, != and == are converted to ne and eq to perform
diff -Naur Template-Toolkit-2.06d/parser/Parser.yp Template-Toolkit-2.06d-flush/parser/Parser.yp
--- Template-Toolkit-2.06d/parser/Parser.yp Thu Nov 29 17:44:36 2001
+++ Template-Toolkit-2.06d-flush/parser/Parser.yp Thu Mar 7 18:41:30 2002
@@ -120,6 +120,7 @@
| RETURN { $factory->return() }
| STOP { $factory->stop() }
| CLEAR { "\$output = '';"; }
+ | FLUSH { "\$context->output(\$output); \$output = '';"; }
| LAST { $_[0]->{ INFOR } || $_[0]->{ INWHILE }
? 'last LOOP;'
: 'last;' }
--------------010101090402050104070103--