Velocity模板引擎 - 键 - 值映射 [英] Velocity Template engine - key-value-map

查看:625
本文介绍了Velocity模板引擎 - 键 - 值映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题を使用键 - 值映射到速度。

有人有此功能的一个例子吗?

  $ MYMAP = {}$ myMap.put(mykey1,myvalue的)
$ myMap.delete(mykey1)
$ myMap.getValue(mykey1)


解决方案

由于森说,你应该使用:

 #设置($ MYMAP = {})

要创建一个新的地图,并将其分配给一个变​​量。

现在,为什么是调用打印出来。


  1. 任何不指令里面,像 #SET(不打印)#如果(不打印) #foreach(同样不打印),打印,包括自由文本,变量和方法调用


  2. 速度不能 $ myMap.get('的myKey') $ myMap.put(钥匙进行区分, '值'),所以调用的结果打印出来。


  3. 当东西不能被正确评估,因为沿线的变量没有定义或某处方法返回中,code,它未能进行评估字面上倾倒到输出。


  4. 随着法状态的文件,该函数返回存储该键的previous值,或,如果没有设置值的。


  5. 总结这一切,这是正常得到该行打印出来。


要出去试试这个理论,你可以这样做:

 #设置($ MYMAP = {})
$ myMap.put(钥匙,第一个值)
$ myMap.put('键','第二个值)
$ myMap.get(钥匙)

这将打印:

  $ myMap.put(钥匙,第一个值)
第一个值
第二个值

有两件事情可以做,使线不打印:


  1. 存储功能在一个临时变量的结果:#设置($丢弃= $ myMap.put('键','值')


  2. 使用无声的方法调用:<!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.

  1. 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.

  2. Velocity can't distinguish between $myMap.get('mykey') and $myMap.put('key', 'value'), so the result of the put call is printed.

  3. 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.

  4. 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.

  5. 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:

  1. Store the outcome of the function in a temporary variable: #set ($discard = $myMap.put('key', 'value')

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆