Hello monks, I'm very new to HTML template and am trying to output a table from a hash. Simplified code:
my %trial_hash = ( ABC => "john", DEF => "mike", );
My template is like so:
my $tmpl = HTML::Template->new(scalarref => \ <<EO_TMPL <!DOCTYPE HTML> <html><head><title>Table</title></head> <body> <table><thead> <tr><TMPL_LOOP TH><th><TMPL_VAR CELL></th></TMPL_LOOP></tr> </thead> <tbody><TMPL_LOOP TD><td><TMPL_VAR CELL></td></TMPL_LOOP> </tbody></table></body></html> EO_TMPL );
And the code I'm trying to use to populate is:
$tmpl->param( TH => [ map { CELL => $_ }, qw( Type Value ) ], TD => [ map { CELL => $_ }, (each %trial_hash), ], );
The output is
<!DOCTYPE HTML> <html><head><title>Table</title></head> <body> <table><thead> <tr><th>Type</th><th>Value</th></tr> </thead> <tbody><td>ABC</td><td>john</td> </tbody></table></body></html>
So it's only using one value from the trial hash. I assume that the problem is I need another loop to go through each key value pair of the hashes. So if I put in another loop for the rows:
<tbody><TMPL_LOOP TR><tr> <TMPL_LOOP TD><td><TMPL_VAR CELL></td></TMPL_LOOP> </tr></TMPL_LOOP> </tbody></table></body></html>
then im sure this way to populate:
TR => [ { TD => [ map { CELL => $_ }, (each %trial_hash) ] }, { TD => [ map { CELL => $_ }, (each %trial_hash) ] }, ],
Is completely wrong! Also, I dont know how many key/val pairs are going to be in the hash. How would I go about populating this? The data structures required to populate templates are really confusing me. I've been reading guides but cant seem to get it into my head. Any pointers are greatly appreciated!

In reply to Populating a html template problem by mavericknik

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.