Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: HTML::Template Accessing Params and Reassigning

by snoopy (Deacon)
on Aug 18, 2009 at 00:13 UTC ( #789311=note: print w/ replies, xml ) Need Help??


in reply to HTML::Template Accessing Params and Reassigning

A concise working code example is always best!

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;
$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.
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.

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:

#! /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;
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.

Update: Added loop_context_vars => 1 to HTML::Template->new(...). Now getting exactly the same error message as the OP.


Comment on Re: HTML::Template Accessing Params and Reassigning
Select or Download Code
Re^2: HTML::Template Accessing Params and Reassigning
by Rodster001 (Monk) on Aug 18, 2009 at 16:26 UTC
    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.

    Thanks Snoop! That was it... for some reason I was thinking that by assigning it to an array it would dereference it. But, looking at it again this morning I am not sure where my mind was :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://789311]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-07-22 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (109 votes), past polls