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

my $class = $line & 1 ? "odd" : "even";

or even

my $class = $. & 1 ? "odd" : "even";

and get rid of $line :-)

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^3: creating HTML table
by GertMT (Hermit) on Jul 16, 2007 at 10:12 UTC
    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>
      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 ...
      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/
      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