This will give you an idea of what your data structure looks like. In this case, it is a two dimensional array, or an array of array references. Moving ahead, let's now build a template to work with:use strict; use HTML::Template; use Data::Dumper; my @array = ( [(1..5)], [qw(one two three four five)], [qw(ein zwei drei veir funf)], [qw(hana dool set net dasut)], [qw(yi er san si wu)], ); print Dumper \@array;
So, in order for this to work, we need a need to pass the the param() method one arg named 'rows', and that "key" must point to a list of hash references. Each of these hash references will have a one key, 'cells'. For now, let's just focus on the first list of hash references. We will use map to transform the list of array references from @array into a list of hash refs. Each key will be called 'cells' and the values will be each of the inner array references from @array. Read from right to left:<table> <tmpl_loop rows> <tr> <tmpl_loop cells> <td><tmpl_var data></td> </tmpl_loop> </tr> </tmpl_loop> </table>
The whole map statement must be 'wrapped' by brackets to specify that $rows is really an array reference.my $rows = [ map {{ cells => $_ }} @array ]; print Dumper $rows;
Now all we need to do is turn each of those elements into yet another hash ref, yet another single key, 'data':
That is what HTML::Template wants - each loop must be a list of hash references. And yes, this is not easy stuff! :) (This is also why i wrote DBIx::XHTML_Table, but that is another story.) Here a complete script that you can play with. Good luck! :)my $rows = [ map {{ cells => [ map {{ data => $_ }} @$_ ] }} @array ]; print Dumper $rows;
use strict; use HTML::Template; use Data::Dumper; my @array = ( [(1..5)], [qw(one two three four five)], [qw(ein zwei drei veir funf)], [qw(hana dool set net dasut)], [qw(yi er san si wu)], ); my $rows = [ map {{ cells => [ map {{ data => $_ }} @$_ ] }} @array ]; my $data = do {local $/;<DATA>}; my $tmpl = HTML::Template->new( scalarref => \$data, ); $tmpl->param( rows => $rows ); print $tmpl->output; __DATA__ <table> <tmpl_loop rows> <tr> <tmpl_loop cells> <td><tmpl_var data></td> </tmpl_loop> </tr> </tmpl_loop> </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)
In reply to (jeffa) Re: HTML table with HTML::Template and loop in a loop
by jeffa
in thread HTML table with HTML::Template and loop in a loop
by fireartist
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |