Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

(dkubb) Re: (2) alternating row colors

by dkubb (Deacon)
on May 18, 2001 at 09:04 UTC ( #81433=note: print w/replies, xml ) Need Help??

in reply to alternating row colors

IMHO, I'm not a big fan of having my perl code mixed with HTML code. When I really began seperating my logic (perl code) from my presentation code (HTML and the template language), my web-based apps became infinitely simpler to maintain, and develop.

To get this seperation I use HTML::Template. With this module, you can alternate the colors for each row of data, all from within the template.

Here's a simple example that will alternate between two colors inside a template:

<!-- TMPL_LOOP NAME="rows" --> <!-- TMPL_IF NAME="__ODD__" --> #FFFFFF <!-- TMPL_ELSE --> #DDDDFF <!-- /TMPL_IF --> <!-- /TMPL_LOOP -->

(Of course, in the a real-world this would contain more HTML code, but I didn't want to clutter the example)

Then I'd use the following code to load the template, loop through the rows, print the results, and alternate the colors on each iteration:

my $template = HTML::Template->new( filename => 'template_name.tmpl', loop_context_vars => 1, ); $template->param(rows => $dbh->selectall_hashref($statement)); print $cgi->header, $template->output;

Now, you could do this inside perl code, but I really believe that when you begin to include things other than perl code inside your perl scripts, they become less maintainable, and less elegant.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2022-12-06 01:34 GMT
Find Nodes?
    Voting Booth?

    No recent polls found