30

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

目前模板引擎将“”转换为,所以我必须这样做。

#set ( $a = "")
#if ($a) 
   assert("never prints a neither gets here: " + $a)
#end

有没有办法我可以直接做到这一点?我希望能够做出类似的东西:

This is the variable $a. ## in case that $a is null i don't want 'dollar a' to be displayed
4

3 回答 3

54

$!a成功了。您可以直接使用此表格而无需if检查。

于 2012-09-04T13:54:50.670 回答
24

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

这是您的示例:

This is the variable $!a.

如果$anull"",Velocity 将呈现:

This is the variable .

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

于 2014-02-14T12:14:20.733 回答
1

另一种选择是根据Checking for Nullif修改您的语句(感谢链接@xavi-lópez):

方法 2:使用 null 在安静引用中被评估为空字符串的事实。(参见 http://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation

因此,您的代码将是:

#set ( $a = "")
#if ("$a" != "") 
   assert("never prints a neither gets here: " + $a)
#end
于 2018-07-31T22:07:00.663 回答