You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Sam Joseph <ga...@yha.att.ne.jp> on 2002/06/25 07:13:24 UTC

Reversing through a loop

Hi there,

Quick question - is there anyway to reverse iterate through a loop in
Velocity, i.e . something like

#foreach( $mud in $mudsOnSpecial )

and

#foreach( $mud in reverse $mudsOnSpecial )

where the output respectively would be

mud1, mud2, mud3

and

mud3, mud2, mud1

There probably isn't but I thought I'd ask.

Thanks in advance.

CHEERS> SAM


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Reversing through a loop

Posted by Michael Pearson <pe...@intecgroup.com.au>.
Sam Joseph wrote:
> Quick question - is there anyway to reverse iterate through a loop in
> Velocity, i.e . something like
> 
> #foreach( $mud in $mudsOnSpecial )
> 
> and
> 
> #foreach( $mud in reverse $mudsOnSpecial )
> 
> There probably isn't but I thought I'd ask.

Not natively, no. However, if $mudsOnSpecial is a List:

  #foreach( $foo in $mudsOnSpecial )
    #set( $index = $mudsOnSpecial.size() - $velocityCount )
    Mud: $mudsOnSpecial.get($index)
  #end

Hackish, but it should work. Assumes that $velocityCount is your 
velocity counter, and that $velocityCount will begin at 1. Don't stress 
if you're not sure what that means: they're the defaults, anyway.

The better way to do it would be to create a mudsOnSpecialReversed in 
your Java code, put it into the request, and iterate normally.

A 'reverse' directive would be useful, though.

-- 
Michael Pearson
Intec Consulting Group
Tel: +61 8 8359 2332
pearson@intecgroup.com.au


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>