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.


In reply to (dkubb) Re: (2) alternating row colors by dkubb
in thread alternating row colors by qball

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.