Why not just have a directive for this, as it's probably a common functionality (similar to what webmacro does)
#alternate ( $color through [ "red", "blue" ] ) #foreach ( $item in $list ) $color #end So, $color just evaluates to the current color, and moves to the next one, cycling back to the beginning when necessary. I'd be willing to write the directive for it. -----Original Message----- From: Claude Brisson [mailto:[EMAIL PROTECTED] Sent: Sunday, May 02, 2004 1:12 PM To: Velocity Developers List Subject: Re: Foreach cycle Hi, it seems a little complex... Why not use the modulo function ? #foreach($item in $list) #if($velocityCount%2==0) #set($color="#aaaaaa") #else #set($color="#eeeeee") #end <tr bgcolor="$color"> <td>$item</td> </tr> #end CloD ----- Original Message ----- From: "evik" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Sunday, May 02, 2004 6:19 PM Subject: Foreach cycle > Hi! > > I usually print my tables that have every other row a little > different color for easier reading. I did not find any such > thing dirrectly supported in velocity so I made a little patch. > It extends foreach like this: > #foreach ( $item in $list ["#aaaaaa", "#eeeeee"] as $color ) > <tr bgcolor="$color"> > <td>$item</td> > </tr> > #end > > It will render every odd row with bgcolor="#aaaaaa" and the > even ones with bgcolor="#eeeeee". I thinkahelps to the > designer or the developper who had to solve this issue > with macro logic. > > If anyone is interested I put up a patch against the current > CVS version at: > http://chaos.hu/foreach_cycle.patch.gz > > I wrote it if the ( $item in $list ) paramters are correct, and > the cycling parameters have any problem, the code will not drop > any errors, but render like if only the first two args would be > given. This is for not rendering a page useless because someone > tinkered around he cycling list. Also note that this logic makes > the extended parameters optional, so the old style works just as > it should. > > Also I "discovered" a rendering problem during this development. > If anyone accidentally gives another parameter to #foreach, the > render code would threat the fourth parameter as the rendering > template. This patch also fix this by not rendering to a fix numbered > child but always to invoke rendering on the last child. > > Any feedback is appreciated. > > evik > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
