Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Populating a html template problem

by mavericknik (Sexton)
on Feb 09, 2016 at 19:09 UTC ( [id://1154758]=perlquestion: print w/replies, xml ) Need Help??

mavericknik has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: Populating a html template problem
by jeffa (Bishop) on Feb 09, 2016 at 19:45 UTC

    You have to adjust your data structure AND the template:

    use strict; use warnings; use Data::Dumper; use HTML::Template; my %hash = ( ABC => "john", DEF => "mike", GHI => "mary", ); my $tmpl = HTML::Template->new(scalarref => \q{ <table> <thead> <tr><TMPL_LOOP TH> <th><TMPL_VAR CELL></th></TMPL_LOOP> </tr> </thead> <tbody><TMPL_LOOP TD> <tr> <td><TMPL_VAR KEY></td> <td><TMPL_VAR VAL></td> </tr></TMPL_LOOP> </tbody> </table> }); $tmpl->param( TH => [ map { CELL => $_ }, qw( Type Value ) ], TD => [ map { KEY => $_, VAL => $hash{$_} }, keys %hash ], ); print $tmpl->output;

    Output:

    <table> <thead> <tr> <th>Type</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>ABC</td> <td>john</td> </tr> <tr> <td>DEF</td> <td>mike</td> </tr> <tr> <td>GHI</td> <td>mary</td> </tr> </tbody> </table>

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      That works perfectly, thank you very much! Cant believe I didnt think of using 2 variables in the template.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1154758]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found