in reply to Perl Template + html: hash keys and values keyword not working
[% FOREACH item IN report.rows.values %] <tr> <td>[% item.id %]</td> <td>[% item.delta %] %</td> </tr> [% END %]
The values of the report.rows hash are not themselves hashes, but arrays. Therefore item.id makes no sense. item.0.id is more likely to be what you are after.
If you want to iterate over all items in the arrays you'll need a nested loop or some other means of iterating inside them. eg.
#!/usr/bin/env perl use strict; use warnings; use Template; my $r = { 'rows' => { 'category1' => [ {'id' => 'AAA', 'delta' => '0.01%'}, {'id' => 'BBB', 'delta' => '0.03%'} ], 'category2' => [ {'id' => 'CCC', 'delta' => '0.02%'}, {'id' => 'DDD', 'delta' => '0.04%'} ], }, }; my $text = q# [% FOREACH item IN report.rows.values %] [% FOREACH one IN item %] <tr> <td>[% one.id %]</td> <td>[% one.delta %] %</td> </tr> [% END %] [% END %] #; my $tt = Template->new; $tt->process (\$text, {report => $r});
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl Template + html: hash keys and values keyword not working
by feiiiiiiiiiii (Acolyte) on May 08, 2016 at 02:22 UTC | |
by Bod (Parson) on Mar 12, 2024 at 23:34 UTC | |
by afoken (Chancellor) on Mar 13, 2024 at 19:34 UTC |