2010-08-13 75 views
34

我已經瞭解了使用速度1.6中的$ null通過由您更新的資源進行空檢查。 資源:Reading model objects mapped in Velocity Templates 但是我面臨着太多的挑戰,因爲沒有提供關於這個的文檔,所以沒有null null的空速檢查速度。 請提供給我一些文檔,說明$ null對於空檢速度是有效的。

由於提前 幸運

回答

73

要檢查一個變量是不是空只需使用#如果($變量)

 
#if ($variable) 
... do stuff here if the variable is not null 
#end 

如果你需要做的東西,如果變量爲空根本否定test

 
#if (!$variable) 
... do stuff here if the variable is null 
#end 
+9

如果是布爾值,這也會返回true。這是一個很好的空檢查文檔:http://wiki.apache.org/velocity/CheckingForNull – 2011-09-16 18:13:57

+0

跟隨@TJ評論,任何想法如何導入最近的'NullTool'?它似乎不在速度工具2.0中。 – ecoe 2018-02-05 04:51:32