velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Lee <Robt....@rcn.com>
Subject Re: #foreach($i in [1..$n]) causes exception
Date Mon, 30 May 2011 20:00:43 GMT
I have tried
#foreach($i in [1..$n])
Where $n is set into the context as

context.put("$n", rs.getString("n"));

oops.  I should have caught it myself.

context.put("$n", rs.getInt("n"));  did the trick

Thanks for your insight.

On 5/30/2011 2:57 PM, Sergiu Dumitriu wrote:
> 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.
>
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 9.0.901 / Virus Database: 271.1.1/3669 - Release Date: 05/30/11 02:34:00
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message