I have some problems wo use a key-value-map into Velocity.
Someone has an example of this functionality?
$myMap ={}
$myMap.put("mykey1", "myvalue")
$myMap.delete("mykey1")
$myMap.getValue("mykey1")
Velocity templates were deprecated in Liferay Portal 7.0 and are now removed in favor of FreeMarker templates in Liferay DXP 7.2.
Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.
As Nathan said, you should use:
#set ($myMap = {})
to create a new map and assign it to a variable.
Now, why is the put
call printed.
Anything that is not inside a directive, like #set(not printed)
or #if(not printed)
or #foreach(again not printed)
, is printed, including free text, variables, and method calls.
Velocity can't distinguish between the different semantics of $myMap.get('mykey')
and $myMap.put('key', 'value')
(reader vs. writer), so the result of the put
call is printed, just like any other method.
Whenever something can't be properly evaluated, because a variable is not defined or somewhere along the line a method returns null
, the code that failed to be evaluated is dumped literally into the output.
As the documentation of the put
method states, the function returns the previous value stored for that key, or null
if no value was set at all.
Summing it all up, it's normal to get that line printed.
To try this theory out, you can do this:
#set ($myMap = {})
$myMap.put('key', 'first value')
$myMap.put('key', 'second value')
$myMap.get('key')
This will be printed:
$myMap.put('key', 'first value')
first value
second value
There are two things you can do so that the line isn't printed:
Store the outcome of the function in a temporary variable: #set ($discard = $myMap.put('key', 'value')
Use the silent method call: $!myMap.put('key', 'value')
I'd recommend the first one, since the second one will still print something when you're replacing an existing value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With