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

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

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