I really like the OUTPUT => sub { } config option for templates. The *only*
thing is I'd like to pass a variable into the calling  routine to avoid
using a package global. My suggestion is include both the template object
and the $vars reference to target output routine.

Below is my patch - including docs patch. :)

This is against TT2 2.06d.

J

diff -Naur Template-Toolkit-2.06/lib/Template/Manual/Config.pod
Template-Toolkit-2.06.j2/lib/Template/Manual/Config.pod
--- Template-Toolkit-2.06/lib/Template/Manual/Config.pod Wed Nov  7 09:47:53
2001
+++ Template-Toolkit-2.06.j2/lib/Template/Manual/Config.pod Tue Feb 12
17:07:48 2002
@@ -849,8 +849,9 @@
 working directory if not specified absolutely); a file handle
 (e.g. GLOB or IO::Handle) opened for writing; a reference to a text
 string to which the output is appended (the string isn't cleared); a
-reference to a subroutine which is called, passing the output text as
-an argument; or as a reference to any object that supports the print()
+reference to a subroutine which is called, passing the output text,
+template object, and process vars as arguments;
+or as a reference to any object that supports the print()
 method.  This latter option includes the Apache::Request object which
 is passed as the argument to Apache/mod_perl handlers.

@@ -878,7 +879,12 @@

 example 4 (subroutine):

-    sub output { my $out = shift; print "OUTPUT: $out" }
+    sub output {
+        my $out = shift;
+        my $template = shift;
+        my $vars = shift;
+        print "OUTPUT: $out"
+    }

     my $template = Template->new({
  OUTPUT => \&output,
diff -Naur Template-Toolkit-2.06/lib/Template.pm
Template-Toolkit-2.06.j2/lib/Template.pm
--- Template-Toolkit-2.06/lib/Template.pm Wed Nov  7 09:47:52 2001
+++ Template-Toolkit-2.06.j2/lib/Template.pm Tue Feb 12 17:06:30 2002
@@ -67,7 +67,7 @@

  # send processed template to output stream, checking for error
  return ($self->error($error))
-     if ($error = &_output($outstream, $output));
+     if ($error = $self->_output($vars, $outstream, $output));

  return 1;
     }
@@ -125,17 +125,17 @@


 #------------------------------------------------------------------------
-# _output($where, $text)
+# _output($self, $vars, $where, $text)
 #------------------------------------------------------------------------

 sub _output {
-    my ($where, $text, $binmode) = @_;
+    my ($self, $vars, $where, $text, $binmode) = @_;
     my $reftype;
     my $error = 0;

     # call a CODE referenc
     if (($reftype = ref($where)) eq 'CODE') {
- &$where($text);
+ &$where($text, $self, $vars);
     }
     # print to a glob (such as \*STDOUT)
     elsif ($reftype eq 'GLOB') {




Reply via email to