in reply to HTML table with HTML::Template and loop in a loop
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: HTML table with HTML::Template and loop in a loop
by fireartist (Chaplain) on Sep 19, 2002 at 15:28 UTC | |
by fglock (Vicar) on Sep 19, 2002 at 19:10 UTC | |
by blokhead (Monsignor) on Sep 19, 2002 at 18:37 UTC |