in reply to Loop Context Style with HTML::Template

I am with Bird on this one gryphon (although i do really like fruiture's suggestion). Here is why - right now you have two colors for two kinds of rows, but what happens when someone in charge decides that they want three colors, or four or five? __ODD__ isn't going to cut it anymore. By utilizing the modulus (%) operator, you can easily loop through a list of 'classes'. Consider this (and replace the single quotes with double quotes if you are running this on Win32 system):
perl -le '@a = a..d; print $a[$i++ % @a] for 0..15'
Couple this with a map and you can add a different class for every row of data. Now, i don't agree with storing the style sheet info inside the script, after all, you want the others in your group to maintain that part. Having said all of that, here is my example:
use strict; use HTML::Template; my $data = do {local $/; <DATA>}; my $tmpl = HTML::Template->new(scalarref => \$data); my $rows = [ { col1 => 'Graham Chapman', col2 => 'John Cleese' }, { col1 => 'Terry Gilliam', col2 => 'Eric Idle' }, { col1 => 'Terry Jones ', col2 => 'Michael Palin' }, { col1 => 'Terry Jones ', col2 => 'Michael Palin' }, { col1 => 'Terry Gilliam', col2 => 'Eric Idle' }, ]; my $i = 0; my @class = qw(one two three); $tmpl->param( rows => [ map { { %$_, class => $class[$i++ % @class] } } @$rows ], ); print $tmpl->output; __DATA__ <style type="text/css"> <!-- table { border-spacing: 0px; } td { padding: 3px; } td.one { background-color: #c0c0c0; } td.three { background-color: #e0e0e0; } td.two { background-color: #d0d0d0; } --> </style> <table> <tmpl_loop rows> <tr> <td class="<tmpl_var class>"> <tmpl_var col1> </td> <td class="<tmpl_var class>"> <tmpl_var col2> </td> <tr> </tmpl_loop> </table>
So, the code is a little more complex, but not terribly more and definitely at the expense of making the template less complex. I am sure the others in your group will appreciate it. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)