Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Alternating colors for HTML Table's

by narainhere (Monk)
on Sep 06, 2007 at 10:28 UTC ( [id://637375]=perlquestion: print w/replies, xml ) Need Help??

narainhere has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I would like to share the wisdom I gained.We come across situations when we need to generate a HTML table and stuff some data into it.If the table contains large number of rows we would use bi-(or)multi-color to make it easier to read and for some visual appeal. We can do it in a line of code

@colors=("blue","green"); foreach $row(@rows) { print "<TR BGCOLOR=".$colors[push(@colors,shift(@colors))- +@colors].">".$row."</TR>"; }
This keeps on swapping the colors for each row and code too will look neat and simple.Any more simplification welcomed!!

Be inspired by Perl REGEX when you choose your spouse!!

Replies are listed 'Best First'.
Re: Alternating colors for HTML Table's
by marto (Cardinal) on Sep 06, 2007 at 11:18 UTC
    I try to keep HTML seperate from my code, using HTML::Template and <TMPL_IF NAME="__first__">,<TMPL_IF NAME="__odd__"> etc you can achieve this sort of result. Check out the documentation from examples.

    Hope this helps

    Update:Slight rewording and link to the docs.

    Martin
Re: Alternating colors for HTML Table's
by ctilmes (Vicar) on Sep 06, 2007 at 10:45 UTC
    my @colors = qw(blue green); my $i = 0; foreach my $row (@rows) { print "<TR BGCOLOR=$colors[$i++ % @colors]>$row</TR>"; }
Re: Alternating colors for HTML Table's
by moritz (Cardinal) on Sep 06, 2007 at 11:29 UTC
    Timtowtdi:

    use Tie::Cycle; tie my $color, 'Tie::Cycle', [qw(red green blue)]; for my $row (@rows){ print qq{<tr bgcolor="$color">$row</tr>\n}; }

    But normally you use a template anyway...

      TIMTOWDI I like that!!!!

      The world is so big for any individual to conquer

Re: Alternating colors for HTML Table's
by hawtin (Prior) on Sep 06, 2007 at 14:17 UTC

    TIMTOWTDI**2

    Personally I don't like setting format in the HTML, I find it easier to manage in the stylesheet.

    In site.css: .row_2 { background-color: #f4f4f4; } .row_1 { background-color: #ffffff; } In generate.pl: ... print "\@import url(/site.css);\n"; my $num_rowtypes = 2; ... my $count = 0; for(my $i=0;$i<=$#{$items_list};$i++) { print "<tr class=\"row_${count}\">". "<td class=\"pos\">".($i+1)."</td>\n"; foreach my $col ("art","tit","yer","cht") { my $val = link_to($cat,$page,$col,$nam); print "<td class=\"$col\">".$val."</td>\n"; } print "</tr>\n"; $count++; $count = 0 if($count == $num_rowtypes); }
Re: Alternating colors for HTML Table's
by Lorphos (Novice) on Sep 06, 2007 at 14:29 UTC
    Yet another WTDI:

    Data::Table has builtin support for different colors for even and odd rows.

Re: Alternating colors for HTML Table's
by Anonymous Monk on Sep 06, 2007 at 10:57 UTC
    @colors=("blue","green"); for my $row(0..$#rows) { print "<TR BGCOLOR=", $colors[ $row %2], ">", $rows[$row],"</TR>"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found