I would like to propose a new feature: die_on_bad_includes.  If set to
0, then the <tmpl_include> statement does not require the file to
actually exist.  The attached patch implements this functionality, but
I haven't tested it with caching, and suspect it may have problems
there.  Since I have no experience using the caching features I
figured it would be safer to not try to patch that area.

Why would anyone want this?  Maybe a case study will make it clear.  I
have a website where some of the pages are drawn by a Perl module that
I wrote.  This module uses HTML::Template to display these pages.  But
in some parts of the site there is a disclaimer to be added to the
bottom of the page, and not in others.  At first I satisfied this need
by having a blank disclaimer in the directory at the end of the search
path, and this can be overridden by putting the disclaimer in a
directory earlier in the path if it was needed.  However I soon
realized that I would need to do the same for several other files as
well, and I didn't want a bunch of blank files in the directory that
is at the end of the path.  So I modified HTML::Template to take care
of it for me, and the results are attached:


cd /work/wrw/HTML-Template-2.4-patched/
diff -bBdituw /work/wrw/HTML-Template-2.4 
/work/wrw/HTML-Template-2.4-patched/Template.pm
--- /work/wrw/HTML-Template-2.4/Template.pm     Mon Dec 31 11:53:12 2001
+++ /work/wrw/HTML-Template-2.4-patched/Template.pm     Mon Dec 31 12:18:09 2001
@@ -451,6 +451,12 @@
 
 =item *
 
+die_on_bad_includes - if set to 0 the module will let you use
+<TMPL_INCLUDE nonexistent.tmpl> in your template, even if the file
+'nonexistent.tmpl' does not exist.  Defaults to 1.
+
+=item *
+
 strict - if set to 0 the module will allow things that look like they might be TMPL_* 
tags to get by without dieing.  Example:
 
    <TMPL_HUH NAME=ZUH>
@@ -857,6 +863,7 @@
                shared_cache_debug => 0,
                memory_debug => 0,
                die_on_bad_params => 1,
+               die_on_bad_includes => 1,
                vanguard_compatibility_mode => 0,
                associate => [],
                path => [],
@@ -1026,6 +1033,7 @@
                debug => 0,
                stack_debug => 0,
                die_on_bad_params => 1,
+               die_on_bad_includes => 1,
                associate => [],
                loop_context_vars => 0,
               );
@@ -1477,6 +1485,7 @@
         $template->{options}{debug} = $options->{debug};
         $template->{options}{stack_debug} = $options->{stack_debug};
         $template->{options}{die_on_bad_params} = $options->{die_on_bad_params};
+        $template->{options}{die_on_bad_includes} = $options->{die_on_bad_includes};
         $template->{options}{case_sensitive} = $options->{case_sensitive};
 
         push(@pstacks, $template->{parse_stack});
@@ -1915,6 +1924,7 @@
                                            param_map => $param_map,
                                            debug => $options->{debug}, 
                                            die_on_bad_params => 
$options->{die_on_bad_params}, 
+                                           die_on_bad_includes => 
+$options->{die_on_bad_includes},
                                            loop_context_vars => 
$options->{loop_context_vars},
                                            case_sensitive => 
$options->{case_sensitive},
                                           );
@@ -2021,18 +2031,23 @@
         } else {
           $filepath = $self->_find_file($filename, \@path);
         }
-        die "HTML::Template->new() : Cannot open included file $filename : file not 
found."
-          unless defined($filepath);
+        my $included_template = "";
+        if (!defined($filepath)) {
+          if ($options->{die_on_bad_includes}) {
+            die "HTML::Template->new() : Cannot open included file $filename : file 
+not found. (die_on_bad_includes set => 1)";
+          }
+        }
+        else {
         die "HTML::Template->new() : Cannot open included file $filename : $!"
           unless defined(open(TEMPLATE, $filepath));              
         
         # read into the array
-        my $included_template = "";
         while(read(TEMPLATE, $included_template, 10240, length($included_template))) 
{}
         close(TEMPLATE);
         
         # call filters if necessary
         $self->_call_filters(\$included_template) if @{$options->{filter}};
+        }
         
         if ($included_template) { # not empty
           # handle the old vanguard format - this needs to happen here

Diff finished at Mon Dec 31 12:35:44

-- 
William R Ward            [EMAIL PROTECTED]          http://www.wards.net/~bill/
-----------------------------------------------------------------------------
     If you're not part of the solution, you're part of the precipitate.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to