in reply to Sending data for LOOP in HTML::Template

You'll need to make nested loops, which are kind of a pain in HTML::Template. The data structure is fairly straight-forward, but generating it usually takes some nested maps. The idea is to basically make an array slice of each output column using the @selection array of indices. Here, I've generated nested row and column loops:
use HTML::Template; my @data = ( [ qw/a0 a1 a2 a3 a4 a5/ ], [ qw/b0 b1 b2 b3 b4 b5/ ], [ qw/c0 c1 c2 c3 c4 c5/ ], [ qw/d0 d1 d2 d3 d4 d5/ ] ); my @col_name = qw/ col0 col1 col2 col3 col4 col5 /; my @selection = (1, 4, 5); my $tmpl = HTML::Template->new( filehandle => \*DATA ); my @headers = map {{ title => $_ }} @col_name[ @selection ]; my @rows = map {{ cols => [ map {{ data => $_ }} @$_[ @selection ] +] }} @data; $tmpl->param( headers => \@headers, rows => \@rows ); print $tmpl->output; __DATA__ <table border=1> <tr> <TMPL_LOOP headers><th><TMPL_VAR title></th></TMPL_LOOP> </tr> <TMPL_LOOP rows> <tr> <TMPL_LOOP cols><td><TMPL_VAR data></td></TMPL_LOOP> </tr> </TMPL_LOOP> </table>

which produces this output:

col1col4col5
a1a4a5
b1b4b5
c1c4c5
d1d4d5

If you're getting this data from a database, though, you can probably let the database handle the slicing of each row... either by using selectall_arrayref and its Slice option, or by just listing the required columns in the query.

blokhead

Replies are listed 'Best First'.
Re: Re: Sending data for LOOP in HTML::Template
by Steny (Sexton) on May 30, 2004 at 20:30 UTC
    Here's some sample code that doesn't work.
    My main problem is that I don't understand what data type is suppose to be send to the TMPL_LOOP in the template, and don't understand how to create & modify the data type before sending it...
    At least, I'm 99% sure that's my problem.

    my @checkedOptions = qw(team rank land networth strat updated mari +ne fighter bomber panzer hl seal pds offPts defPts turns); my @tableHeaders; foreach my $i (@checkedOptions) { push @tableHeader, $i; }

    That places the selected options into an array. then this:
    my $tableHeader = tableHeader(@tableHeader);

    is used to send that array to a subroutine, that I would like to create an appropriate data type for the TMPL_LOOP and then store the appropriate data to it.

    Then subroutine I was attempting was this:
    sub tableHeader { my @tableHeader = $_[0]; my $rows; my %tempHash; foreach my $i (@tableHeader) { if ($i eq 'nw') { push @{$rows}, {columnHeader => 'Networth'} } elsif ($i eq 'hl') { push @{$rows}, {columnHeader => 'Heavy Lasers'} } elsif ($i eq 'offPts') { push @{$rows}, {columnHeader => 'Off. Pts'} } elsif ($i eq 'defPts') { push @{$rows}, {columnHeader => 'Def. Pts'} } elsif ($i eq 'last_updated') { push @{$rows}, {columnHeader => 'Updated'} } else { $tempHash{'columnHeader'} = capitalize($i); push @{$rows}, {columnHeader => $i} } } return $rows; }

    I was then hoping to juse use the following in a HTML::Template file to create the appropriate column headers:
    <TMPL_LOOP NAME=tableHeader> <td align=center valign=middle><B>&nbsp;<TMPL_VAR NAME=columnH +eader>&nbsp;</B><TD> </TMPL_LOOP>
    Basically I was hoping to send an array of hashes (each hash only containing 'columnHeader => columnName') to the template, and then to loop through each of them to pull the column names that were selected.

    I tried disecting the code I use to pull data from a table and send it to the template, but obviously went about it the wrong way ;-P

    Any help is greatly appreciated.

    If I wasn't clear in explaining what I was trying to do, please let me know and I'll gladly attempt to re-explain in a more clear manner.

    I'm still relatively new to some of the more complex (at least, more complex for me) parts of HTML::Template, and am trying to do the coding right the first time, rather then having to recode everything in the future :-)


    Thanks again!


    Steny

    20040530 Edit by Corion: Fixed formatting, moved stuff into CODE tags

      What you need is an AoH containing further AoHs. A small example:
      #!/usr/bin/perl use strict; use warnings; use HTML::Template; # some fruits my $fruits = [ { fruit => "banana", properties => [ {where_from => 'europe', size=> 'small'}, {where_from => 'africa', size=> 'large'}, ] }, { fruit => "apple", properties => [ {where_from => 'asia', size=> 'medium'}, {where_from => 'america', size=> 'large'}, ] }, ]; # and now let's add another fruit push @$fruits, { fruit => 'strawberry'}; # let us add some properties ${fruits}->[2]->{properties} = [ {where_from => 'europe', size=> 'small'}, {where_from => 'new zealand', size=> 'huge'}, ]; my $tmpl = HTML::Template->new( filehandle => \*DATA ); $tmpl->param( fruits => $fruits ); print $tmpl->output; __DATA__ <TMPL_LOOP NAME="fruits"> <table> <tr> <th><TMPL_VAR NAME="fruit"></th> <th></th> </tr> <TMPL_LOOP NAME="properties"> <tr> <td><TMPL_VAR NAME="where_from"></td> <td><TMPL_VAR NAME="size"></td> </tr> </TMPL_LOOP> </table> </TMPL_LOOP>
      I suggest you the intensive use of Data::Dumper. It helps you to see the structure of your AoH etc.