??之间的区别,has_content,if_exists存在于freemarker中 [英] Difference between ?? , has_content , if_exists in freemarker
问题描述
freemarker中的以下内容之间有什么区别?
what is the difference between the following in freemarker?
!
has_content
??
if_exists
我用过??代替has_content&它导致了如此巨大的问题.搞砸了我的一天.我真的需要弄清楚这件事.
I used ?? instead of has_content & it lead to such huge issues. Screwed up my day. I really need to get this thing clarified.
使用时
!(xyz.abc!)?? -- it dint work
使用时
!(xyz.abc!)?has_content ... it did work
不是吗?或has_content或if_exists检查同一件事?
Doesn't ?? OR has_content OR if_exists check for the same thing?
推荐答案
??
告知左操作数的值是否丢失(意味着它是Java null
或您那里有未定义的变量),并返回true
(不丢失).
??
tells if the left hand operand's value is missing (means it's Java null
or you have an undefined variable there), and gives back false
(missing) or true
(not missing) accordingly.
?has_content
非常类似于??
,不同之处在于它还会为长度为0的字符串或空FTL序列(如java.util.List
,Java数组等)或空FTL哈希(如java.util.Map
等). (对于0
,布尔值false
等,它不会返回false
)
?has_content
is very much like ??
, except it also returns false
for a 0-length string or empty FTL sequence (like java.util.List
, Java array, etc.) or empty FTL hash (like java.util.Map
, etc.). (It doesn't return false
for a 0
, boolean false
, etc.)
!
用于在缺少值(再次表示它是Java null
或您有未定义的变量)时提供默认值,例如color!"no color"
.如果省略!
的右侧操作数,则默认值为同时为空字符串,空序列和空哈希(多类型值),这对于编写<#list things! as thing>
,${foo!}
.
!
is used to give a default value when a value is missing (again means that it's Java null
or you have an undefined variable), like color!"no color"
. If you omit the right hand operand of !
, then the default value is an empty string and empty sequence and empty hash on the same time (a multi-typed value), which is handy for writing things like <#list things! as thing>
, ${foo!}
.
?if_exists
是编写??
的旧方法.不要使用它.
?if_exists
is the old way of writing ??
. Don't use it.
虽然我们在这里,但请注意,所有这些运算符仅覆盖点分或[]
表达式的最后一步,例如user.price!0
仅在user
中缺少price
时处理,而在user
中不处理.本身不见了.为了涵盖这两种可能性,请使用(user.price)!0
,它可以处理在评估()
期间抛出的所有丢失的变量错误,而不论它们来自何处.
While we are here, note that all these operators only cover the last step of a dotted or []
expression, like user.price!0
only handles if price
is missing from user
, but not if user
itself is missing. To cover both possibilities, use (user.price)!0
, which handles all missing variable errors thrown during the evaluation of the ()
, no mater where they come from.
这篇关于??之间的区别,has_content,if_exists存在于freemarker中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!