Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by atcroft (Monsignor)
on Aug 31, 2002 at 21:34 UTC ( #194390=perlquestion: print w/ replies, xml ) Need Help??
atcroft has asked for the wisdom of the Perl Monks concerning the following question:

My fellow and esteemed Monks, I come to thee penitent, in search of wisdom and guidance.

Background: While I have tried to become a better Monk and user of perl in general, I have harbored a dark, embarassing secret sin in my coding, which I must now confess: Only recently have I begun to take my first faltering steps in learning to use a CPAN-available templating system. To my shame, I have generally embedded the HTML of many of my CGI scripts internally, either with print statements or, recently, with HERE documents, and only once did I venture so far into the darkness as to write my own simplistic attempt at a templating system. Once I learned about the available templating systems, and was able to get one of them installed on the system on which most of those CGIs run, it was my goal to someday go back and recode those still in use to use templates, so as to ease their maintenence.

"Someday" has now become now, as I have been asked to modify the "look and feel" of the scripts in order to make them more closely match that of the rest of the systems in use. While most of them involve minor changes that can wait, a "due date" has been set on about a dozen of the worst offenders (in terms of radically different appearance). These dozen or so happen to be some of the oldest of the CGIs, so it seemed logical to attempt to migrate them to templates for the future. I have now run into a problem, which I seek your guidance on.

Problem: Several of the scripts display data in an almost condensed form in tables, which I do not know how to replicate using HTML::Template. A simplified example-I may have returned a dozen or so values to display, and display them four to a line, so I may be returned (1, 3, 4, 6, 7, 8, 10, 12, 13, 14, 15, 19, 23, for instance) and need to display them as:

1346
781012
13141519
23   

My question is, do I have to do something in the code to do this, or is there a way in HTML::Template that I can have it only display the number of items per line I need (it would be a fixed number for that page, if that makes it easier)? Would embedded loops or included templates do this, or would they be more complicated than necessary?

I am truly outside of my knowledge range at the moment as I try to learn to do this, and to learn to be a better coder. Your attention and wisdom are greatly appreciated.

Comment on A question regarding HTML::Template, tables, and loops
Re: A question regarding HTML::Template, tables, and loops
by LTjake (Prior) on Aug 31, 2002 at 22:08 UTC
    Hi. You'll want to check out jeffa's reply to one of my own questions.

    This little snippet is the key:
    use strict; use Data::Dumper; my ($i,$j) = (0,0); my $tab; for (0..10) { push @{$tab->[$j]},$_; $j++ unless ++$i % 4; } print Dumper $tab;
    which returns this:
    $VAR1 = [ [ 0, 1, 2, 3 ], [ 4, 5, 6, 7 ], [ 8, 9, 10 ] ];
    You could then just nest a loop within a loop.

    HTH.
Re: A question regarding HTML::Template, tables, and loops
by rattusillegitimus (Friar) on Sep 01, 2002 at 03:10 UTC

    I asked a similar question, and got some good responses. My problem was slightly different; I was trying to make a series of tables with two rows each and each having no more than X columns. I suspect the solutions I received are adaptable, and I know they worked well for me.

    --
    $rattusillegitimus = Eliza::PerlMonks::Robot::new()

Re: A question regarding HTML::Template, tables, and loops
by samtregar (Abbot) on Sep 02, 2002 at 02:28 UTC
    There are any number of ways to solve this problem with HTML::Template, but here's how I'd do it. Here's the script:

    use HTML::Template; my $template = HTML::Template->new(filename => "test.tmpl"); my @data = (1 .. 30); my $items_per_row = 5; my $x = 0; my @loop; foreach (@data) { my %row = ( data => $_ ); $row{break} = 1 unless $x % $items_per_row or $x == 0; push @loop, \%row; $x++; } $template->param(loop => \@loop); print $template->output();

    And the corresponding template:

    <table> <tr> <tmpl_loop loop> <tmpl_if break></tr><tr></tmpl_if> <td><tmpl_var data></td> </tmpl_loop> </tr> </table>

    Does that help?

    -sam

Re: A question regarding HTML::Template, tables, and loops
by gryphon (Abbot) on Sep 02, 2002 at 05:57 UTC

    Greetings atcroft,

    One of the things I've noticed about HTML tables is that in some browsers you have to specify blank cells with "&nbsp;" 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;

Re: A question regarding HTML::Template, tables, and loops
by Anonymous Monk on Sep 03, 2002 at 23:12 UTC
    You may be interested in checking out the Template Toolkit at www.template-toolkit.org. IT's got a nice tables module, which you can set to push out the tables in a few different formats, set how many rows/cols etc. It may be overkill for what you're doing, and my experience with HTML::Template is kinda limited, but I highly reccomend it if you're going to be doing lots of work with templates.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://194390]
Approved by Mr. Muskrat
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-09-19 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls