[Templates] wrapper in control?

Andy Wardley abw@cre.canon.co.uk
Thu, 24 Aug 2000 09:42:11 +0100 (BST)


> I still don't think there's an easy way to do what I want to do, so
> I'll implement the patch under my SUPRESS_PROCESS flag and let
> Andy have it for inclusion or not.  You can just not use the flag. :)

Sorry to not reply sooner, I've been away for a few days.

When I added the PRE_PROCESS and POST_PROCESS options, I thought about 
also adding the PROCESS option.  At the time, I was only considering adding
it for completeness, but I couldn't think of a good use for it, so didn't
bother.  However, this sounds like exactly what you need.

If you specify a PROCESS template (or templates) then it gets processed
instead of your main template.  The original template is still available
as the 'template' reference so you can examine metadata items and INCLUDE
or PROCESS it if you want.

For example:

    my $tt = Template->new({
	PRE_PROCESS  => 'header',
	PROCESS      => 'body',
	POST_PROCESS => 'footer',
    });
    $tt->process('myfile.html');

header: 
    
    [% template.title or 'Default Title' %]
    
    

body:
    author: [% template.author %]        # access metadata
    [% PROCESS $template %]              # process main template
    # etc...

footer:
    

If you want to suppress PROCESS altogether, then you can specify PROCESS
as undef, an empty string or empty array ref.  Here is a patch against 
Service.pm to implement this.  Let me know if this isn't up to scratch 
for what you need, or just go ahead and fix it and send me a patch.  

Cheers
A



diff -u -r2.0 Service.pm
--- Service.pm	2000/08/10 14:56:04	2.0
+++ Service.pm	2000/08/24 09:07:19
@@ -83,11 +83,13 @@
 
 	# PROCESS
 	eval {
-	    $procout = $context->process($template);
+	    foreach $name (@{ $self->{ PROCESS } || [ $template ] }) {
+		$procout .= $context->process($name);
+	    }
 	};
 	if ($error = $@) {
 	    last SERVICE
-		unless defined ($procout = $self->_recover(\$error));
+		unless defined ($procout .= $self->_recover(\$error));
 	}
 	$output .= $procout;
 
@@ -130,14 +132,17 @@
     my ($self, $config) = @_;
     my ($item, $data, $context, $block, $blocks);
 
-    # coerce PRE_PROCESS and POST_PROCESS to arrays if necessary, 
+    # coerce PRE_PROCESS, PROCESS and POST_PROCESS to arrays if necessary, 
     # by splitting on non-word characters
-    foreach $item (qw( PRE_PROCESS POST_PROCESS )) {
+    foreach $item (qw( PRE_PROCESS PROCESS POST_PROCESS )) {
 	$data = $config->{ $item };
 	$data = [ split(/\W+/, $data || '') ]
 	    unless ref $data eq 'ARRAY';
         $self->{ $item } = $data;
     }
+    # unset PROCESS option unless explicitly specified in config
+    $self->{ PROCESS } = undef
+	unless exists $config->{ PROCESS };
     
     $self->{ ERROR      } = $config->{ ERROR } || $config->{ ERRORS };
     $self->{ AUTO_RESET } = defined $config->{ AUTO_RESET }