Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: A question regarding HTML::Template, tables, and loops

by gryphon (Abbot)
on Sep 02, 2002 at 05:57 UTC ( #194531=note: print w/ replies, xml ) Need Help??


in reply to A question regarding HTML::Template, tables, and loops

Greetings atcroft,

One of the things I've noticed about HTML tables is that in some browsers you have to specify blank cells with " " or else something yucky looking gets put into the final display. So the code of my attempt to answer your question inserts blank cell data for whatever remaining cells are in the table.

use strict; my @set_of_stuff = (1,3,4,6,7,8,10,12,13,14,15,19,23); my $items_per_row = 4; my @table_data; for (0 .. scalar @set_of_stuff / $items_per_row) { my @row_data = map {$_={cell_data=>$_ }} splice(@set_of_stuff,0,$ite +ms_per_row); push @row_data, {cell_data=>'&nbsp'} while scalar @row_data < $items +_per_row; push @table_data, { row_data => \@row_data }; }

Also, I really like letting the brilliance of the module handle what data goes where in what type of table. I lean away from mucking around with the template itself (i.e. inserting "break" variables to set the rows) and rather prefer to let the module setup the table via the data. Gryphon's personal rule of HTML::Template: Put as much of the "thinking" into the Perl as possible leaving a simpler template.

use HTML::Template; my $template = q{ <TABLE border=1><TMPL_LOOP name="table_data"> <TR><TMPL_LOOP name="row_data"> <TD><TMPL_VAR name="cell_data"></TD> </TMPL_LOOP></TR> </TMPL_LOOP></TABLE> }; my $template = HTML::Template->new(scalarref => \$template, option => +'value'); $template->param(table_data => \@table_data); print $template->output();

General aside: When I was first playing around with HTML::Template, I was really annoyed with having to deal with all the references required. However, once I got used to it, I've come to realize that the elegance and brilliance of the module is all about putting all the "thinking" in your Perl and only the HTML in the templates. I'm amazed at what the author's done just about every time I use it.

gryphon
code('Perl') || die;


Comment on Re: A question regarding HTML::Template, tables, and loops
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://194531]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2014-12-26 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (168 votes), past polls