velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nathan Bubna <nbu...@gmail.com>
Subject Re: Property to turn off velocity from echoing reference
Date Wed, 25 Feb 2009 15:02:44 GMT
On Mon, Feb 23, 2009 at 12:39 AM, veerabahu subramanian
<veechand@gmail.com> wrote:
> Hi All,
> I am trying to do a collection manipulation in Velocity, i.e. I will check
> for existence of key in a HashMap and if it exists I will append the current
> value if not I will add the current value.
> The template file for this is as below
>
> [code]
> #set($dummymap={})
> #foreach($listItem in $list)
> #if($dummymap.get($listItem))
> ##if the value exists in the map
> #set($existingvalue=$dummymap.get($listItem))
> #set($existingvalue="${existingvalue},$listItem")
> #set($dontuse=${dummymap.put($listItem,$existingvalue)})
> #else
> $!{dummymap.put($listItem,"1")}
> #end
> #end
> Dummy map is $dummymap
> [/code]
>
> In this I have two doubts
>  1. If I didn't assign the return value
> of ${dummymap.put($listItem,$existingvalue)} to a variable then the return
> value is rendered as a output. Is there any property to turn off this
> behavior. I did a search for the same also checked the velocity.properties
> in latest build but I don't find a relevant one.

Use quiet notation:
$!{dummymap.put($listItem, $existingvalue)}

or you can set up a ReferenceInsertionEventHandler that watches for
null references that you would rather have render as empty string than
the reference text.  but that's more complicated.

>  2. Will dummymap treated as a string internally by velocity. Mainly I want
> to know will there be a performance improvement if i do the above logic
> inside "#foreach($listItem in $list) " in Java instead of template file

it's always faster to do things in java than Velocity.  not much,
since Velocity is a reflection layer on top of java, but at least a
little.  and in general, it is a good idea to keep model logic out of
your view layer.  if this is purely view logic, you still may find it
better to keep the complicated stuff in a "tool" that does the work in
java instead of VTL.

>
> Thanks,
> Veerabahu
>

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


Mime
View raw message