2014-05-08 80 views
40

freemarker中的以下區別是什麼?之間的區別,has_content,ifmarkists in freemarker

! 
has_content 
?? 
if_exists 

我用過??而不是has_content &它導致如此巨大的問題。搞砸了我的一天。我真的需要澄清這件事。

當我用

!(xyz.abc!)?? -- it dint work 

當我用

!(xyz.abc!)?has_content ... it did work 

不?或has_content或if_exists檢查相同的事情?

+1

見文件http://freemarker.org/docs/dgui_template_exp.html –

回答

86

??告知用戶,如果左手操作數的值丟失(意味着它的Java null或者你有沒有未定義的變量),和false(失蹤)或true(不丟失)相應還給。

?has_content非常像??,除了它也返回false爲空字符串或空列表或空地圖。 (它不爲0返回false,布爾false等)

!是用來給當值是缺少,像color!"no color"默認值。如果省略!的右側操作數,則默認值爲空字符串,同時爲空序列和空散列。

?if_exists是舊的寫作方式??。不要使用它。

+13

「有據可查」 聲明值得一些鏈接:) [has_content(http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_has_content) ,[默認值運算符!](http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing_default),[缺少值測試](http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing_test),和[舊樣式if_exists](http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing) – coolersport

+1

'has_content'是alwarys有用的,除了值爲布爾值或數字1/0是/否' – soytian

+0

如果這位買家不在'<#elseif buyer.company?has_content>'中,那麼它會給undeception如何檢查買家是否存在,我嘗試了以上所有! – shareef