Hello all,
I'm having some trouble with HTML::Template giving me erroneous messages becaues the data I am passing to my methods isn't in arrayref form.
My code:
package PL::Template; use strict; use CGI; use HTML::Template; my $q = CGI->new; sub new { my $class = shift; my $self = {}; $self->{template}; $self->{content}; bless $self, $class; return $self; } sub format { my $self = shift; my %params = @_; $self->{content} = $params{content}; my $filter = sub { my $ref = shift; $$ref =~ s/\[aim:\/\/(.*?)\]/<a href="aim:username=$1">$1<\/a>/g; $$ref =~ s/\[google:\/\/(.*?)\]/<a href="http:\/\/www.google.com\ +/search\?q=$1">$1<\/a>/g; $$ref =~ s/\[email:\/\/(.*?)\]/<a href="mailto:$1">$1<\/a>/g; $$ref =~ s!\[([^|\]]+)\|([^\]]+?)\]!<a href=$1>$2</a>!gs; $$ref =~ s|\[([^\]]+?)\]|<a href=$1>$1</a>|g; }; my $tmpl= HTML::Template->new( filename => "tmpl/main.tmpl", associate =>$q, loop_context_vars =>1, global_vars=>1, filter => $filter ); $tmpl->param( %{ $self->{content} } ); return $q->header, $tmpl->output(); } sub output { my $self = shift; return $self->format; } 1; ##index.cgi #!perl -w use strict; use PL; my $obj = PL->new; my %content = ( title=>'hi', body=>'hello testing' ); my $page = $obj->Template->format (content => \%content); print $obj->Template->output; ##the template <html> <head> <title><!--TMPL_VAR name="title"--></title> </head> <body> <!--TMPL_VAR name="body"--> </body> </html> </readmore> <br /> I have attempted numerous methods of formatting my data into an arrayr +ef, only one of which has worked: <code> $tmpl->param( content => [{ title=>'hi'...body=>'testing' }] );

now as simple as this seems, I can't get my content variable in the correct arrayref format.
HTML::Template says in the docs:
The <TMPL_LOOP> tag is a bit more complicated than <TMPL_VAR>. The <TMPL_LOOP> tag allows you to delimit a section of text and give it a name. Inside this named loop you place <TMPL_VAR>s. Now you pass to param() a list (an array ref) of parameter assignments (hash refs) for this loop. The loop iterates over the list and produces output from the text block for each pass. Unset parameters are skipped. Here's an example: In the template: <TMPL_LOOP NAME=EMPLOYEE_INFO> Name: <TMPL_VAR NAME=NAME>
Job: <TMPL_VAR NAME=JOB>

</TMPL_LOOP> In the script: $template->param(EMPLOYEE_INFO => { name => 'Sam', job => 'programmer' }, { name => 'Steve', job => 'soda jerk' }, ); print $template->output(); The output in a browser: Name: Sam Job: programmer Name: Steve Job: soda jerk
Any ideas?
Thanks in advance.

meh.

In reply to HTML::Template data types problem by stonecolddevin

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.