无法在速度模板中设置 Null

Unable to set Nulll in Velocity Template

我正在尝试在 Velocity 中将变量设置为 null。我正在尝试:

#set ($acessSpeed = null)

我正在阅读 velocity null 支持 wiki。它说我们可以通过这种方式将值设置为空。 https://wiki.apache.org/velocity/VelocityNullSupport

但是当我尝试它时,我收到一个错误提示 "Encountered "null" at ...."

我遇到的问题是我有一个带有多个 if 块的巨大模板,如果满足条件,就会执行这些块。所以在每个 if 块的末尾,我需要将 accessSpeed 的值设置为 null。

#if (some condition)
     access speed value set here.
.
.
.
#end
// I need to set the access speed value to null here.
#if (some other condition)
    access speed value to be set to something again.
.
.
.
#end

我可以为每个 if 块使用不同的变量,但我想知道是否有更简单的方法来做到这一点。

任何建议都会有所帮助。

这取决于您的配置。做你需要的,你需要配置速度:

directive.set.null.allowed = true

然后,您可以将变量设置为空:

#set($foo = $null)

其中 $null 只是一个未定义的变量。

否则,如果唯一目的是测试变量,那么一个方便的技巧是将其值设置为 false。

#set($foo = false)
#if($foo) this will be displayed #end