velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergiu Dumitriu <ser...@xwiki.com>
Subject Re: #foreach($i in [1..$n]) causes exception
Date Mon, 30 May 2011 18:57:41 GMT
On 05/30/2011 08:36 PM, Robert Lee wrote:
> I have tried
> #set($n = 2)
> #set($range = "[1..$n]")
> #foreach($i in $range)
> as well Velocity 1.7 fails when it reaches the range variable in the
> #foreach directive.
>
> Is there a way to accomplish what I am trying to do, (a variable count
> loop) in velocity?

The error is that you're not actually iterating over a range, but trying 
to iterate over a string.

#set($range = "[1..$n]") => $range is a String
#foreach($i in $range) => $range is still a String, it won't be replaced 
with [1..$n]

This should work:

#set($n = 2)
#foreach($i in [1..$n])

If you must define the range somewhere else as a string, you can use 
#evaluate to dynamically interpret the range, but that adds unneeded 
complexity.
-- 
Sergiu Dumitriu
http://purl.org/net/sergiu/

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


Mime
View raw message