Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.


In reply to Re: HTML::Template Accessing Params and Reassigning by snoopy
in thread HTML::Template Accessing Params and Reassigning by Rodster001

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (15)
    As of 2014-07-10 10:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (205 votes), past polls