Velocity模板引擎 - 键 - 值映射 [英] Velocity Template engine - key-value-map
问题描述
我有一些问题を使用键 - 值映射到速度。
有人有此功能的一个例子吗?
$ MYMAP = {}$ myMap.put(mykey1,myvalue的)
$ myMap.delete(mykey1)
$ myMap.getValue(mykey1)
由于森说,你应该使用:
#设置($ MYMAP = {})
要创建一个新的地图,并将其分配给一个变量。
现在,为什么是把
调用打印出来。
-
任何不指令里面,像
#SET(不打印)
或#如果(不打印)
或#foreach(同样不打印)
,打印,包括自由文本,变量和方法调用 -
速度不能
$ myMap.get('的myKey')
和$ myMap.put(钥匙进行区分, '值')
,所以把
调用的结果打印出来。 -
当东西不能被正确评估,因为沿线的变量没有定义或某处方法返回
空
中,code,它未能进行评估字面上倾倒到输出。 -
随着
把
法状态的文件,该函数返回存储该键的previous值,或空
,如果没有设置值的。 -
总结这一切,这是正常得到该行打印出来。
要出去试试这个理论,你可以这样做:
#设置($ MYMAP = {})
$ myMap.put(钥匙,第一个值)
$ myMap.put('键','第二个值)
$ myMap.get(钥匙)
这将打印:
$ myMap.put(钥匙,第一个值)
第一个值
第二个值
有两件事情可以做,使线不打印:
-
存储功能在一个临时变量的结果:
#设置($丢弃= $ myMap.put('键','值')
-
使用无声的方法调用:<!code> $ myMap.put('键','值')
我建议你第一个,因为第二个仍然会打印,当你更换现有的有价值的东西。
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")
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
$myMap.get('mykey')
and$myMap.put('key', 'value')
, so the result of theput
call is printed.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, ornull
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.
这篇关于Velocity模板引擎 - 键 - 值映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!