in reply to Loop Context Style with HTML::Template

I would definitely go with option 3, but why not take it one step further? Rename your "__ODD__" template variable to something like "class", then store the full class name instead of just a "1" or "" in that variable. Your HTML then looks like this...
<STYLE type="text/css"><!-- TD.loop1 { background-color: #c0c0c0; } TD.loop { background-color: #e0e0e0; } --></style> <TABLE border=0 cellspacing=0 cellpadding=3> <TMPL_LOOP name="some_loop"> <TR> <TD class=<TMPL_VAR name="class">> <TMPL_VAR name="content_column_1"> </td> <TD class=<TMPL_VAR name="class">> <TMPL_VAR name="content_column_2"> </td> <TR> </TMPL_LOOP> </table>
At this point you can just alternate between values of "loop" or "loop1" in the "bg_class" key in your loop array. If you want to take that one step further, you could pull the class names and background colors themselves into your script, and replace them with template variables in the HTML. This way you could use an array of class name/color pairs which would be accessed by both the style section and the loop data structure. This would allow you to expand to an arbitrary number of background colors to be cycled through. Something like...
<STYLE type="text/css"><!-- <TMPL_LOOP name="style_loop"> TD.<TMPL_VAR name="class"> { background-color: <TMPL_VAR name="color"> +; } --></style>
...for the style section. And...
my @styles = ( { class => "bg_red", color => "#FF0000" }, { class => "bg_green", color => "#00FF00" }, { class => "bg_blue", color => "#0000FF" } );
...for the styles array. Then the main loop would use $styles[$n]->{class} to fill it's data structure, with $n cycling through each element of @styles.

Whoa... okay, so I got a little carried away here. This is probably more than you need, but that's where I'd go with it.

-Bird

Replies are listed 'Best First'.
Re: Re: Loop Context Style with HTML::Template
by gryphon (Abbot) on Aug 02, 2002 at 21:50 UTC

    I would definitely go with option 3, but why not take it one step further? Rename your "__ODD__" template variable to something like "class", then store the full class name instead of just a "1" or "" in that variable.

    Greetings Bird,

    The problem with that is that the <TMPL_VAR name="__ODD__"> is a loop context variable. It's created and controlled by HTML::Template as the module loops through the array passed to it by reference. I don't think I can mess with it without violating some OOP law.

    I could define "class" as an element of the hash of the array of the "some_loop" loop, but one of the reasons I liked the HTML::Template concept was that it didn't require me to do any such defining since the module's code handled everything.

    -gryphon
    code('Perl') || die;

      Right, sorry, I didn't realize you were so set on using the loop context variable. By creating one of my own, I provided a more versatile solution which is, as I mentioned, probably more than you need.

      Oh well... :)

      -Bird