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!