velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Denis <j...@respublica.fr>
Subject Re: question about loops
Date Tue, 06 Aug 2002 15:09:09 GMT

On Tuesday, August 6, 2002, at 11:05  am, Geir Magnusson Jr. wrote:

> The problem we are trying to avoid is runaway loops :
>
>   #while(true)
>     ...
>   #end
>
> Or
>
>   #for( $I= 0; $I < 10; $I = $I -1 )
>    ...
>   #end
>
> With a foreach, it's awfully hard to make simple mistakes that 
> lead to these
> kinds of problems.

With foreach, there can't be infinite loops except for badly 
programmed iterators. But that shouldn't stop the template 
designers to implement a loop limited to 10 items...

If I'd wanted to display 10 first items of a list, I would like to 
be able to do something like:


#set ($indexes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#foreach ($index in $indexes)
	#display( $items[index] )
#end


Or with the #break directive:


#foreach ($item in $items)
	#display( $item )
	#if ($velocityCount = 10)
		#break
	#end
#end


There was also the proposal of the composed foreach:


#set ($indexes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#foreach ($index in $indexes, $item in $items)
	#display( $item )
#end


-- Denis.

>
> --
> Geir Magnusson Jr.
> Research & Development, Adeptra Inc.
> geirm@adeptra.com
> +1-203-247-1713


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


Mime
View raw message