Velocity nulls和空字符串

Jor*_*.S. 25 java templates velocity

在速度我有一个变量,其值为null.在这种情况下,我不想显示任何内容.

目前模板引擎将""转换为null,所以我必须这样做.

#set ( $a = "")
#if ($a) 
   assert("never prints a neither gets here: " + $a)
#end
Run Code Online (Sandbox Code Playgroud)

有没有办法直接做到这一点?我希望能够做出类似的事情:

This is the variable $a. ## in case that $a is null i don't want 'dollar a' to be displayed
Run Code Online (Sandbox Code Playgroud)

Irm*_*mak 44

$!a诀窍.您可以直接使用此表单而无需检查.


Den*_*isS 19

你想要安静的参考符号:$!a

这是你的例子:

This is the variable $!a.
Run Code Online (Sandbox Code Playgroud)

如果$ a为null或"",则Velocity将呈现:

This is the variable .
Run Code Online (Sandbox Code Playgroud)

官方指南部分:https: //velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation