Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: creating HTML table

by GertMT (Hermit)
on Jul 16, 2007 at 10:12 UTC ( [id://626806]=note: print w/replies, xml ) Need Help??


in reply to Re^2: creating HTML table
in thread creating HTML table

Thanks for all input. With the code mentioned below that is inspired by your input and some article re. HTML::template on perl.com I've got HTML::template creating a nice table with odd/even rows. Just one thing to work on is how if it were not a single column table. Guess I've got to read the csv that's providing the data for the table into a hash. That'll be a challenge again.
#!/usr/bin/perl -w use strict; use diagnostics; use HTML::Template; open( OUT, ">fruit.html" ) or die "cant open out $!"; my $html = do{local $/;<DATA>}; my $tpl = HTML::Template->new( scalarref => \$html, loop_context_vars => 1, filter => \&cstmrow_filter ); my @rows = ( "Apple", "Orange", "Mango", "Kiwi" ); my @loop_data = (); # initialize an array to hold my loop while (@rows) { my %row_data; # a hash for my data list $row_data{FILES} = shift @rows; push( @loop_data, \%row_data ); } $tpl->param( rows => \@loop_data ); print OUT $tpl->output; sub cstmrow_filter { my $text_ref = shift; $$text_ref =~ s/<CSTM_ROW\s+EVEN=(.+)\s+ODD=(.*)\s*> /<TMPL_IF NAME=__odd__> <tr class="$1"> <TMPL_ELSE> <tr class="$2"> <\/TMPL_IF> /gx; } __DATA__ <head> <style type="text/css"> .odd { background-color: #d3d3d3 } .even { background-color: #90ee90 } </style> </head> <body> <p> <table class="center"> <TMPL_LOOP NAME=rows> <CSTM_ROW EVEN=even ODD=odd> <td> <TMPL_VAR NAME=FILES></td> </tr> </TMPL_LOOP> </table> </p> </body> </html>

Replies are listed 'Best First'.
Re^4: creating HTML table
by Cody Pendant (Prior) on Jul 16, 2007 at 10:29 UTC
    You are making things really really difficult which HTML::Template can make simple.

    You don't need any of that <CSTM> nonsense, or the filter callback.

    Here's a template which does all you want:

    <table> <tmpl_loop name="rows"> <tr <tmpl_if name="__odd__"> class="odd" <tmpl_else> class="even" </tmpl_if> > <td> <tmpl_var name="data"> </td> </tr> </tmpl_loop> </table>

    You put the odd-even logic inside the opening TR tag and HTML::Template does the rest. The source code's going to look a bit weird but the browser doesn't care.



    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
Re^4: creating HTML table
by holli (Abbot) on Jul 16, 2007 at 10:39 UTC
    Hi.

    The code
    while (@rows) { my %row_data; # a hash for my data list $row_data{FILES} = shift @rows; push( @loop_data, \%row_data ); }
    is needlessly destructive to @rows. It can be simplified to
    for my $row (@rows) { push( @loop_data, {FILES => $row} ); }
    or
    for (@rows) { push( @loop_data, {FILES => $_} ); }
    or even
    @loop_data = map { {FILES => $_) } @rows;


    holli, /regexed monk/
Re^4: creating HTML table
by Cody Pendant (Prior) on Jul 16, 2007 at 12:33 UTC
    When you say you read an article on perl.com I assume it was this one. I thought that article was very strange, and it seemed to miss the point of HTML::Template entirely.

    It starts from the premise that if you have to use conditionals in HTML::Template, you'll be tearing your hair out, which I've never found to be the case, and it goes on to propose over-complicated solutions which spoil the simplicity, no matter how long-winded, of HTML::Template's approach, and create obscured code which would make it hard for an HTML coder and a Perl coder to work together, one of the key advantages of any good templating system.

    Throw in the fact that the someone writing an "Advanced" perl article on perl.com doesn't even know that you can use delimiters other than /// for regexes and the whole effect is decidedly strange.



    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
      That was indeed the article I've got part of the code from. I'll have a good study on all replies I go and will be wiser then. To be honest, I currently have almost no hair left as I haven't been able to read in data from a csv file to create a multicolumn table!

      Thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (None)
    As of 2024-04-25 00:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found