【freemarker】常用空判断??和has_content的区别

原始数据
{
    "name1": "",
    "name2": null,
    "list1": [],
    "list2": null
}
模板代码
1.??

属性不存在或者属性为null时返回false

${name1???string}    <#--true-->
${name2???string}    <#--false-->
${name3???string}    <#--false-->
${list1???string}    <#--true-->
${list2???string}    <#--false-->
${list3???string}    <#--false-->
2.has_content

与??的区别是:增加排除空串或者空集合的能力

${name1?has_content?string}      <#--false-->
${name2?has_content?string}      <#--false-->
${name3?has_content?string}      <#--false-->
${list1?has_content?string}      <#--false-->
${list2?has_content?string}      <#--false-->
${list3?has_content?string}      <#--false-->
3.!

相当于三目运算符,通过??判断后,有值就用原始数据的值没有就使用!后面的值(!后面没值使用空)

${name1!123}                 <#--空串-->
${name2!123}                 <#--123-->
${(list1![1,2])?size}        <#--0-->
${(list2![1,2])?size}        <#--2-->
4.if_exists

??的旧表达式,不要使用,输出结果很模糊,可以自行测试

参考

Difference between ?? , has_content , if_exists in freemarker

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值