Steny has asked for the wisdom of the Perl Monks concerning the following question:

Continuing on my journey of struggles w/ doing a little more complicated HTML::Template work, I find myself with this problem...

I have a page that displays a table of data. The user is given the option of selecting which columns of data will be displayed.

I'm trying to keep my code as neat as possible. I know I could just have a VAR in the template, and do a ton of if statements in the template file, to see if each column was selected, but I would rather do it as a LOOP. It will save my template file at least 40 lines of code, and keep things a lot neater.

Simple example/explanation:

A table has 5 optional columns via checkboxes. The user checks off the boxes, clicks submit, then the appropriate columns are displayed when the page is generated.

If the user selects columns 2, 4 and 5, the page is generate showing a table with only those 3 columns.

Earlier in the file, I store the selections in an array.
So...
my @selections = (2, 4, 5);

Column names:
1 = column number 1
2 = column number 2
3 = column number 3
4 = column number 4
5 = column number 5

could someone suggest some code as how I could create the appropriate variable to send to the HTML Template so that it can loop through them?

ie. how would you store the selected column header's names into $rows, so that the following would work?

my $template=HTML::Template->new(filename=>"tmpl/bottom.tmpl");
$template->param(ROWS => $rows);
print $template->output;


Any help would be GREATLY appreciated!

*worships the Perl Monks*


Stenyj
  • Comment on Sending data for LOOP in HTML::Template

Replies are listed 'Best First'.
Re: Sending data for LOOP in HTML::Template
by blokhead (Monsignor) on May 30, 2004 at 19:14 UTC
    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

      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.
Re: Sending data for LOOP in HTML::Template
by neniro (Priest) on May 30, 2004 at 19:00 UTC
    Working with TMPL_LOOP means working with AoHs. This one shows how to change an AoH to add selected items out of an array.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $aoh = [ {col => "col1", val => "val1"}, {col => "col2", val => "val2"}, {col => "col3", val => "val3"}, {col => "col4", val => "val4"}, {col => "col5", val => "val5"}, ]; my @selected = (3, 4, 1); for my $item (@selected) { ${aoh}->[$item]->{selected} = "selected"; } print Dumper($aoh);
    Add_on: if you just want to see the selected ones you can use TMPL_IF inside your TMPL_LOOP.
Re: Sending data for LOOP in HTML::Template
by Golo (Friar) on May 30, 2004 at 18:59 UTC
    ie. how would you store the selected column header's names into $rows, so that the following would work?
    I am not sure if I got your question right, but assuming that $rows contains a reference to an Array of Hashes, where they keys are your columnnames like:
    my $rows = [ { 'column number 4' => 'data row 1, col 4', 'column number 2' => 'data row 1, col 2', 'column number 5' => 'data row 1, col 5', }, { 'column number 4' => 'data row 2, col 4', 'column number 5' => 'data row 2, col 5', 'column number 2' => 'data row 2, col 2', } ];

    you could do
    my %rownames; $rownames{$_} = $_ for sort keys %{@{$rows}[0]}; unshift @$rows, \%rownames;
    to add an additional row at the top which contains the hash keys as values
    [ { 'column number 2' => 'column number 2', 'column number 5' => 'column number 5', 'column number 4' => 'column number 4' }, # ...