velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Mutating a collection
Date Wed, 17 Nov 2010 21:59:43 GMT
Nathan,

On 11/17/2010 4:44 PM, Nathan Bubna wrote:
> On Wed, Nov 17, 2010 at 1:15 PM, Christopher Schultz
> <chris@christopherschultz.net> wrote:
>> All,
>>
>> I'd like to mutate a collection during template evaluation --
>> essentially, I'd like to call Iterator.remove() during a loop. Is that
>> possible? Does Velocity expose an Iterator or anything like that to the
>> template?
> 
> #set( $iterator = $list.iterator() )
> #foreach( $item in $iterator )
> #if ($item.isBad() )#set( $null = $iterator.remove() )#end
> #end

Duh. I was under the mistaken impression that the documentation
(http://velocity.apache.org/engine/releases/velocity-1.6.4/user-guide.html#Loops)
was accurate when I actually know better: you can loop over an iterator
directly!

>> Or, am I forced to iterate over one collection and add to another one?
>>
>> For that matter, is there a convenient way of creating an empty
>> ArrayList or something similar?
> 
> #set( $list = [] )

Is that documented anywhere? I'm not shocked that I get some kind of
collection from that, but I don't see anything in the users guide that
suggests that the above will give me a mutable collection, and if so,
what kind.

Thanks,
-chris


Mime
View raw message