velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nathan Bubna <nbu...@gmail.com>
Subject Re: Mutating a collection
Date Wed, 17 Nov 2010 22:23:07 GMT
On Wed, Nov 17, 2010 at 1:59 PM, Christopher Schultz
<chris@christopherschultz.net> wrote:
> 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!

Yeah, it'll probably put an annoying warning in the logs about it, but
it works fine.  It's not the kind of thing we typically want to
document, because it's not the kind of thing that people should
generally do in the view layer.  But, we always support more than we
ever recommend around here. :)

>>> 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.

i dunno.  i haven't read through the documentation in years.  i tend
not to need to.  but yes, anytime you create a list in VTL, you are
creating a java.util.ArrayList in all its mutable glory.

> Thanks,
> -chris
>
>

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


Mime
View raw message