[htmltmpl] Patch to fix global_vars and loop problem...
Brought to you by:
samtregar
From: simran <sim...@re...> - 2004-05-12 02:47:41
|
Hi Sam, et al, Here are the details of (what i consider to be a bug anyway) a bug in HTML::Template - its been bought up before, and you have mentioned that it does need fixing, but it annoyed me enough today to produce a patch for it and submit the patch :-) Take the following example: ======================================================================== test.tmpl: ---------- Global Variable gvar is: <tmpl_var name=gvar> Loop: <tmpl_loop name=testloop> loopvar: <tmpl_var name=loopvar> gvar : <tmpl_var name=gvar> </tmpl_loop> ======================================================================== test.pl ------- use strict; use HTML::Template; my $t = new HTML::Template(filename => "test.tmpl", global_vars => 1,) || die; my $testloop = [ { loopvar => "GVar should be set", gvar => "Loop GVar 1" }, { loopvar => "GVar should be blank", gvar => "" }, { loopvar => "GVar should be blank again", gvar => undef }, ]; $t->param(gvar => "global variable"); $t->param(testloop => $testloop); print $t->output(); ======================================================================== When you run test.pl.. you get the following output: ------------------------------------------------------------------------ Global Variable gvar is: global variable Loop: loopvar: GVar should be set gvar : Loop GVar 1 loopvar: GVar should be blank gvar : loopvar: GVar should be blank again gvar : global variable ------------------------------------------------------------------------ Now, the issue is that "gvar" in the third iteration of the loop, "should have been blank"... i explicitely set it to "undef" in the code.. however, since global_vars was on, it ignored my "undef". Here's a patch that fixed the problem (using HTML::Template Version 2.6) (it might not be the most efficient.. i've only quickly written something that seems to work... but of course, you might want to check its efficiency etc...) ======================================================================== --- Template.pm.orig 2004-05-12 12:29:01.000000000 +1000 +++ Template.pm 2004-05-12 12:42:58.000000000 +1000 @@ -2464,6 +2464,11 @@ if (defined($value_type) and length($value_type) and ($value_type eq 'ARRAY' or ((ref($value) !~ /^(CODE)|(HASH)|(SCALAR)$/) and $value->isa('ARRAY')))) { (ref($param_map->{$param}) eq 'HTML::Template::LOOP') or croak("HTML::Template::param() : attempt to set parameter '$param' with an array ref - parameter is not a TMPL_LOOP!"); + foreach my $row (@$value) { + foreach my $key (keys %$row) { + $row->{$key} ||= ""; + } + } $param_map->{$param}[HTML::Template::LOOP::PARAM_SET] = [@{$value}]; } else { (ref($param_map->{$param}) eq 'HTML::Template::VAR') or ======================================================================== simran. |