velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Will Glass-Husain <wgl...@forio.com>
Subject Re: Putting key/value pair into hashmap from velocity template
Date Sun, 23 Jul 2006 02:17:56 GMT
That'll teach me to actually try out syntax before suggesting it.

Thanks, Henning...

Henning P. Schmiedehausen wrote:
> Will Glass-Husain <wglass@forio.com> writes:
>
>   
>> Hi Matthias--
>>     
>
>   
>> Two things.  First, this syntax is not valid:
>> ${jMap.put("tttt123", "test")}
>>     
>
> No. It is valid. Don't listen to Will. ;-) 
>
>   
>> Use one of these two forms this instead
>>     
>
>   
>> $jMap.put("ttt123","test")
>> ${jMap}.put("ttt123","test")
>>     
>
> Uhm, no?!?  ${jMap.put(...)} _is_ correct. 
>
> If you write ${jMap}.put(), you force Velocity to evaluate ${jMap}
> without looking at the surrounding template code. This leads to
> executing jMap.toString() which renders the map as text and then
> adding .put(...) after it.
>
>   
>> 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.
>>     
>
> ... or use the quiet notation $! or use 1.5-dev where you can set a property to
> have "null" as a valid reference. 
>
> 	Best regards
> 		Henning
>
>   

-- 
Forio Business Simulations

Will Glass-Husain
phone (415) 440-7500 x89
mobile (415) 235-4293
wglass@forio.com
www.forio.com


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message