You may find it useful to rename the structure like this:<!-- TMPL_LOOP NAME=loop1 --> # means you must have a hash key 'loop1' with an array ref as its valu +e # in the hash. %loop_data = ( loop1 => [ ..something.. ] ); <!-- TMPL_LOOP NAME=loop2 --> # another loop, so each of these 'somethings' must have a hash key loo +p2 # pointing to a value of some array ref %loop_data = ( loop1 => [ { loop2 => [ ..something.. ] }, { loop2 => [ ..something.. ] }, ... ] ); <!-- TMPL_VAR NAME=var1 --> # now each of these somethings must contain a hash key var1 pointing t +o # some scalar data. %loop_data = ( loop1 => [ { loop2 => [ { var1 => 'data'} ] }, { loop2 => [ { var1 => 'otherdata'} ] }, ... ] );
You can either build up your original structure in this way, or use something like the following to convert to this new structure:%loop_data = ( rows => [ { cols => [ { data => 'data'} ] }, { cols => [ { data => 'otherdata'} ] }, ... ] );
This produces the correct output for me.#!/usr/bin/perl -wT use strict; use HTML::Template; my $html = do { local $/; <DATA> }; my $template = HTML::Template->new(scalarref => \$html); my @data = ( [1, 2, 3, 4, 5], ['one', 'two', 'three', 'four', 'five'], ['ein', 'zwei', 'drei', 'veir', 'funf'], ['hana', 'dool', 'set', 'net', 'dasut'], ['yi', 'er', 'san', 'si', 'wu'], ); my %loop_data; foreach my $array_num (0 .. $#data) { my $array_ref = $data[$array_num]; foreach my $array_index (0 .. $#{@$array_ref}) { $loop_data{rows}[$array_index]{cols}[$array_num]{data} = $arra +y_ref->[$array_index]; } } $template->param(\%loop_data); print $template->output; exit; __DATA__ <html> <body> <table> <!-- TMPL_LOOP NAME=rows --> <tr> <!-- TMPL_LOOP NAME=cols --> <td><!-- TMPL_VAR NAME=data --></td> <!-- /TMPL_LOOP --> </tr> <!-- /TMPL_LOOP --> </table> </body> </html>
blokhead
In reply to Re: HTML table with HTML::Template and loop in a loop
by blokhead
in thread HTML table with HTML::Template and loop in a loop
by fireartist
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |