My guess is that you are seeing something like:
#! /usr/bin/perl use warnings; use strict; use HTML::Template; use Data::Dumper; my $text = "<TMPL_LOOP foo>bar=<TMPL_VAR bar>\n</TMPL_LOOP>"; my $template = HTML::Template->new(scalarref => \$text, loop_context_vars => 1); my @a = ({bar => 10}, {bar => 20}); $template->param(foo => \@a); my @array = $template->param('foo'); print Dumper(@array); $template->param(foo => \@array); $template->output;
HTML::Template's param method is returning a scalar reference, so by assigning the return value to an array, you end up with a single element array. That element being the array reference.$VAR1 = [ { 'bar' => 10 }, { 'bar' => 20 } ]; HTML::Template->output() : fatal error in loop output : Not a HASH ref +erence at /usr/share/perl5/HTML/Template.pm line 3059.
The \@array expression then adds another level of indirection. You end up with an array reference to your one element array; the element being an array reference.
You need to work with a returned array reference. Example follows:
Also be aware of a potential trap with the one argument usage of param method. In the above example, if foo is deleted from the template, this method will returned undef rather than the array. I tend to avoid using it for this reason.#! /usr/bin/perl use warnings; use strict; use HTML::Template; use Data::Dumper; my $text = "<TMPL_LOOP foo>bar=<TMPL_VAR bar>\n</TMPL_LOOP>"; my $template = HTML::Template->new(scalarref => \$text, loop_context_vars => 1); $template->param(foo => [{bar => 10}, {bar => 20}]); my $array_ref = $template->param('foo'); if ($array_ref) { print Dumper($array_ref); print Dumper($array_ref->[1]); # # add a new element # push (@$array_ref, {bar => 30}); $template->param(foo => $array_ref); } print $template->output;
Update: Added loop_context_vars => 1 to HTML::Template->new(...). Now getting exactly the same error message as the OP.
In reply to Re: HTML::Template Accessing Params and Reassigning
by snoopy
in thread HTML::Template Accessing Params and Reassigning
by Rodster001
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |