2014-05-08 79 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