You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Jason St. Louis" <ja...@gravitybound.net> on 2004/06/20 09:42:19 UTC

determining odd/even position in foreach loop

Hi everyone.

Is there a built in way of determining whether the current iteration you 
are on is even or odd in a foreach loop?

I am using alternating colors to display rows in a table more visibly 
and I was just hoping there was some easy way of doing this in velocity, 
something like:

#foreach ($foo in $bar)
	#if ( $velocity-even )
		#set ( $row_class = "blue" )
	#else
		#set ( $row_class = "red" )
	#end
#end

I guess I could do it like this?

#if ( $velocityCount%2 == 0 )
	#set ( $row_class = "blue" )
#else
	#set ( $row_class = "red" )
#end


But is that the best way?

Thanks!
Jason

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: determining odd/even position in foreach loop

Posted by Andy Lee <ag...@earthlink.net>.
On Jun 21, 2004, at 11:43 AM, Nathan Bubna wrote:
> but there's also nothing wrong with just using the modulus operator.

As an aside, I would prefer not to use $velocityCount for this purpose, 
as one of the posted examples did.  I'm uncomfortable with 
$velocityCount in general because it's essentially a keyword whose 
meaning depends on a properties file setting.  One potential problem 
would be the case where I want to use two third-party template 
libraries, one of which is 0-based and one of which is 1-based.

I would have preferred either not having the properties setting or 
having separate keywords for the 0-based and 1-based cases -- maybe 
$velocityCount for a count of the number of times the loop body has 
been entered, and $velocityIndex for the (0-based) index of the current 
loop iteration.

Just my $velocityCount+1 cents -- or is it $velocityCount+2?

--Andy


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: determining odd/even position in foreach loop

Posted by Nathan Bubna <na...@esha.com>.
Jason St. Louis said:
> Is there a built in way of determining whether the current iteration you
> are on is even or odd in a foreach loop?
...

as Will said, we did discuss this some weeks back, and the result was the new
Alternator and AlternatorTool in VelocityTools (but not in a released version
yet):

http://cvs.apache.org/viewcvs.cgi/jakarta-velocity-tools/src/java/org/apache/velocity/tools/generic/

those two classes are improvements on the old alternator support in Velocity:
http://jakarta.apache.org/velocity/api/org/apache/velocity/app/tools/VelocityFormatter.html

but there's also nothing wrong with just using the modulus operator.  it's my
understanding that this problem space is the primary reason that Velocity
supports that operator.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Using Velocity in an application

Posted by Brian Utt <br...@yahoo.com>.
use the

Velocity.mergeTemplate(java.lang.String templateName,
java.lang.String encoding, Context context,
java.io.Writer writer)

where encoding is "UTF-8" (normally).




--- Ilan Azbel <ia...@mdio.net> wrote:
> I am using velocity from an application. As the
> Velocity manual says, I use
> the function mergeTemplate() to create my output.
> 
> However, when I compile, this method seems to be
> deprecated?!?!? What should
> I use instead? Maybe the manual should be
> updated?!?!?
> 
> Ilan
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> velocity-user-help@jakarta.apache.org
> 
> 


=====
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. --Kernighan

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Using Velocity in an application

Posted by Ilan Azbel <ia...@mdio.net>.
I am using velocity from an application. As the Velocity manual says, I use
the function mergeTemplate() to create my output.

However, when I compile, this method seems to be deprecated?!?!? What should
I use instead? Maybe the manual should be updated?!?!?

Ilan


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: determining odd/even position in foreach loop

Posted by Will Glass-Husain <wg...@forio.com>.
Didn't we have a big discussion about this in the list earlier in the year?
I couldn't find the message.  I seem to remember the Velocity Tools people
coming up with something clever.

I like the macro idea though.

WILL

----- Original Message ----- 
From: <Ma...@prg.co.nz>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Sunday, June 20, 2004 9:42 PM
Subject: Re: determining odd/even position in foreach loop


> You could use a #macro.
>
> For example something like:
>
> #macro( isOdd $number $oddText $evenText )#if( 0 == $number%2
> )$evenText#else$oddText#end#end
>
> And the in your loop something like:
>
> #foreach ($foo in $bar)
>         <tr class="#isOdd( $velocityCount "blue" "red" )">
>                 ...
>         </tr>
> #end
>
> mano
>
>
>
>
> "Jason St. Louis" <ja...@gravitybound.net>
> 20/06/2004 19:42
> Please respond to
> "Velocity Users List" <ve...@jakarta.apache.org>
>
>
> To
> velocity-user@jakarta.apache.org
> cc
>
> Subject
> determining odd/even position in foreach loop
>
>
>
>
>
>
> Hi everyone.
>
> Is there a built in way of determining whether the current iteration you
> are on is even or odd in a foreach loop?
>
> I am using alternating colors to display rows in a table more visibly
> and I was just hoping there was some easy way of doing this in velocity,
> something like:
>
> #foreach ($foo in $bar)
>                  #if ( $velocity-even )
>                                  #set ( $row_class = "blue" )
>                  #else
>                                  #set ( $row_class = "red" )
>                  #end
> #end
>
> I guess I could do it like this?
>
> #if ( $velocityCount%2 == 0 )
>                  #set ( $row_class = "blue" )
> #else
>                  #set ( $row_class = "red" )
> #end
>
>
> But is that the best way?
>
> Thanks!
> Jason
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: determining odd/even position in foreach loop

Posted by Ma...@prg.co.nz.
You could use a #macro.

For example something like:

#macro( isOdd $number $oddText $evenText )#if( 0 == $number%2 
)$evenText#else$oddText#end#end

And the in your loop something like:

#foreach ($foo in $bar)
        <tr class="#isOdd( $velocityCount "blue" "red" )">
                ...
        </tr>
#end

mano




"Jason St. Louis" <ja...@gravitybound.net> 
20/06/2004 19:42
Please respond to
"Velocity Users List" <ve...@jakarta.apache.org>


To
velocity-user@jakarta.apache.org
cc

Subject
determining odd/even position in foreach loop






Hi everyone.

Is there a built in way of determining whether the current iteration you 
are on is even or odd in a foreach loop?

I am using alternating colors to display rows in a table more visibly 
and I was just hoping there was some easy way of doing this in velocity, 
something like:

#foreach ($foo in $bar)
                 #if ( $velocity-even )
                                 #set ( $row_class = "blue" )
                 #else
                                 #set ( $row_class = "red" )
                 #end
#end

I guess I could do it like this?

#if ( $velocityCount%2 == 0 )
                 #set ( $row_class = "blue" )
#else
                 #set ( $row_class = "red" )
#end


But is that the best way?

Thanks!
Jason

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org