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.
|
|---|