I've been unable to figure out why PROCESS and INCLUDE are implemented
differently, since the only (documented) difference is that INCLUDE
localizes the stash, while PROCESS does not.  So, I modified Context.pm
such that Template::Context::process and Template::Context::include use
the same code base.  I've attached a patch, tests continue to pass.  I'm
interested in what people think, if anything.

I also (incidentally) fixed a comment typo.

(darren)

-- 
What is ideology but the rationalisation of a vested interest?
Index: Context.pm
===================================================================
RCS file: /template-toolkit/Template2/lib/Template/Context.pm,v
retrieving revision 2.64
diff -c -w -r2.64 Context.pm
*** Context.pm  2002/07/09 14:42:45     2.64
--- Context.pm  2002/07/11 13:44:54
***************
*** 218,224 ****
  
  
  #------------------------------------------------------------------------
! # view(\%coonfig)
  # 
  # Create a new Template::View bound to this context.
  #------------------------------------------------------------------------
--- 218,224 ----
  
  
  #------------------------------------------------------------------------
! # view(\%config)
  # 
  # Create a new Template::View bound to this context.
  #------------------------------------------------------------------------
***************
*** 234,253 ****
  
  #------------------------------------------------------------------------
  # process($template, \%params)    [% PROCESS template   var = val, ... %]
  #
  # Processes the template named or referenced by the first parameter.
  # The optional second parameter may reference a hash array of variable
! # definitions.  These are set before the template is processed by calling
! # update() on the stash.  Note that the context is not localised and 
! # these, and any other variables set in the template will retain their
! # new values after this method returns.
  #
  # Returns the output of processing the template.  Errors are thrown
  # as Template::Exception objects via die().  
  #------------------------------------------------------------------------
  
  sub process {
!     my ($self, $template, $params) = @_;
      my ($trim, $blocks) = @$self{ qw( TRIM BLOCKS ) };
      my (@compiled, $name, $compiled);
      my ($stash, $tblocks, $error, $tmpout);
--- 234,256 ----
  
  #------------------------------------------------------------------------
  # process($template, \%params)            [% PROCESS template   var = val, ... %]
+ # process($template, \%params, $localize) [% INCLUDE template   var = val, ... %]
  #
  # Processes the template named or referenced by the first parameter.
  # The optional second parameter may reference a hash array of variable
! # definitions.  These are set before the template is processed by
! # calling update() on the stash.  Note that, unless the third parameter
! # is true, the context is not localised and these, and any other
! # variables set in the template will retain their new values after this
! # method returns.  The third parameter is in place so that this method
! # can handle INCLUDE calls: the stash will be localized.
  #
  # Returns the output of processing the template.  Errors are thrown
  # as Template::Exception objects via die().  
  #------------------------------------------------------------------------
  
  sub process {
!     my ($self, $template, $params, $localize) = @_;
      my ($trim, $blocks) = @$self{ qw( TRIM BLOCKS ) };
      my (@compiled, $name, $compiled);
      my ($stash, $tblocks, $error, $tmpout);
***************
*** 260,269 ****
--- 263,278 ----
        push(@compiled, $self->template($name));
      }
  
+     if ($localize) {
+       # localise the variable stash with any parameters passed
+       $stash = $self->{ STASH } = $self->{ STASH }->clone($params);
+     } else {
        # update stash with any new parameters passed
        $self->{ STASH }->update($params);
        $stash = $self->{ STASH };
+     }
  
+     eval {
        foreach $name (@$template) {
            $compiled = shift @compiled;
            my $element = ref $compiled eq 'CODE' 
***************
*** 271,281 ****
        : $compiled;
        $stash->set('component', $element);
        
!       # merge any local blocks defined in the Template::Document into our
!       # local BLOCKS cache
        @$blocks{ keys %$tblocks } = values %$tblocks
            if UNIVERSAL::isa($compiled, 'Template::Document')
                && ($tblocks = $compiled->blocks());
        
        if (ref $compiled eq 'CODE') {
            $tmpout = &$compiled($self);
--- 280,295 ----
                : $compiled;
                $stash->set('component', $element);
            
!           unless ($localize) {
!               # merge any local blocks defined in the Template::Document
!               # into our local BLOCKS cache
!               # dlc: Why does this only occur in process() and not in
!               # include()?  I've maintained this split, but it makes
!               # me wonder...
                @$blocks{ keys %$tblocks } = values %$tblocks
                    if UNIVERSAL::isa($compiled, 'Template::Document')
                        && ($tblocks = $compiled->blocks());
+           }
            
            if (ref $compiled eq 'CODE') {
                $tmpout = &$compiled($self);
***************
*** 296,302 ****
--- 310,327 ----
            }
            $output .= $tmpout;
        }
+     };
+     $error = $@;
  
+     if ($localize) {
+       # ensure stash is delocalised before dying
+       $self->{ STASH } = $self->{ STASH }->declone();
+     }
+ 
+     $self->throw(ref $error 
+                ? $error : (Template::Constants::ERROR_FILE, $error))
+       if $error;
+     
      return $output;
  }
  
***************
*** 317,376 ****
  
  sub include {
      my ($self, $template, $params) = @_;
!     my $trim = $self->{ TRIM };
!     my (@compiled, $name, $compiled);
!     my ($stash, $error, $tmpout);
!     my $output = '';
! 
!     $template = [ $template ] unless ref $template eq 'ARRAY';
! 
!     # fetch compiled template for each name specified
!     foreach $name (@$template) {
!       push(@compiled, $self->template($name));
      }
  
-     # localise the variable stash with any parameters passed
-     $stash = $self->{ STASH } = $self->{ STASH }->clone($params);
- 
-     eval {
-       foreach $name (@$template) {
-           $compiled = shift @compiled;
-           my $element = ref $compiled eq 'CODE' 
-               ? { (name => (ref $name ? '' : $name), modtime => time()) }
-               : $compiled;
-           $stash->set('component', $element);
- 
-           if (ref $compiled eq 'CODE') {
-               $tmpout = &$compiled($self);
-           }
-           elsif (ref $compiled) {
-               $tmpout = $compiled->process($self);
-           }
-           else {
-               $self->throw('file', 
-                            "invalid template reference: $compiled");
-           }
- 
-           if ($trim) {
-               for ($tmpout) {
-                   s/^\s+//;
-                   s/\s+$//;
-               }
-           }
-           $output .= $tmpout;
-       }
-     };
-     $error = $@;
-     # ensure stash is delocalised before dying
-     $self->{ STASH } = $self->{ STASH }->declone();
-     $self->throw(ref $error 
-                ? $error : (Template::Constants::ERROR_FILE, $error))
-       if $error;
-                                       
-     return $output;
- }
- 
- 
  #------------------------------------------------------------------------
  # insert($file)
  #
--- 342,350 ----
  
  sub include {
      my ($self, $template, $params) = @_;
!     return $self->process($template, $params, 'localize me!');
  }
  
  #------------------------------------------------------------------------
  # insert($file)
  #
***************
*** 1453,1455 ****
--- 1427,1430 ----
  =head1 SEE ALSO
  
  L<Template|Template>, L<Template::Document|Template::Document>, 
L<Template::Exception|Template::Exception>, L<Template::Filters|Template::Filters>, 
L<Template::Plugins|Template::Plugins>, L<Template::Provider|Template::Provider>, 
L<Template::Service|Template::Service>, L<Template::Stash|Template::Stash>
+ 

Reply via email to