velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Re: Putting key/value pair into hashmap from velocity template
Date Fri, 21 Jul 2006 13:43:17 GMT

instead of doing a Map-Wrapper I always use a simple macro
to hide the output of a method call:

#macro( call $exp )#if( $exp )#**##end
#call( $jMap.put("ttt123","test") )

The empty comment within the if-statement is to avoid a parser
bug of early velocity versions. I do not know if the current
release allows empty #if(...)#end body.

You can also do
   #set( $dummy = $jMap.put("ttt123","test") )
but this may result in the funny error log: "set with a null RHS"
and I believe it is less performant that just an if-evaluation.
(In 2001 I made a proposal and contribution allowing: #set($!dummy=null)
  to explicitly allow set-operations with null RHS - but it did not
  find its way in. It is a simple patch, and maybe there is more
  interest now).

You might just use the bang "!" character:
but be aware, that if the context had a value already in the "ttt123"
slot, it will echo this previous value - therefore the Map-wrapper
suggestion from Will.


Will Glass-Husain wrote:
> Second, the "put" call works fine in your example.  But since it returns 
> null, it echoes back the velocity reference.   You can test this by 
> doing a get after the put.
> The reason Velocity works this way is that templates are typically used 
> to just display data.  They very rarely change internal state.  (makes 
> for less error-prone programming).  If you want to call "put" from 
> within your template and not have it return null, you'll need to write a 
> Map wrapper which is either defined void or returns an actual value.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message