in reply to HTML table with HTML::Template and loop in a loop

In this case, you want to slice across the arrays -- pick all the ones, then all the twos, etc. So your structure must be converted slightly, basically just swap logical rows with columns in your representation. To determine the structure, remember that all named items in your template must correspond to a hash key in the structure, and that loops correspond to array references. This is how you would logically build up your %loop_data structure, starting with the outer loop and moving inwards:
<!-- 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 may find it useful to rename the structure like this:
%loop_data = ( rows => [ { cols => [ { data => 'data'} ] }, { cols => [ { data => 'otherdata'} ] }, ... ] );
You can either build up your original structure in this way, or use something like the following to convert to this new structure:
#!/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>
This produces the correct output for me.

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
    blokhead, your explanation at the start is certainly helping me to start to understand it, but I'm not quite there yet!

    Unfortunately, if I run your code, I get an error
    Bizarre copy of ARRAY in leave at ./test.cgi line 21, <DATA> line 1.
    and I can't figure out what's wrong.

    btw, I'm using a cut 'n' paste of your code.

      There is some talk in google that  Bizarre copy of ... might be caused by a perl 5.6.0 bug. Would you try  perl -v please?

      Are you sure the long lines that have been word-wrapped were fixed after copying over? The copy and paste into a new file works for me, but I have my wordwrap setting up higher than default. I've never seen that error before.. Bizarre copy? ;) I wonder what the "in leave" part means.

      blokhead