velocity-user mailing list archives

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

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:
   $!jMap.put("ttt123","test")
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.

Cheers,
Christoph

Will Glass-Husain wrote:
>[snip]
> 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.
> 
> WILL


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


Mime
View raw message