【发布时间】:2013-10-14 19:01:35
【问题描述】:
在 Twig 中,我有运算符 is 并测试空变量(字符串或数组):
{% if info is empty %}
...
{% endif %}
我如何在 Swig 模板中做这样的事情?
【问题讨论】:
标签: swig-template
在 Twig 中,我有运算符 is 并测试空变量(字符串或数组):
{% if info is empty %}
...
{% endif %}
我如何在 Swig 模板中做这样的事情?
【问题讨论】:
标签: swig-template
简单地做
{% if !info.length %}
...
{% endif %}
这将匹配字符串 ("")、数组 ([]) 和任何其他没有 .length 属性和真值的对象。
【讨论】:
{% if Object.keys(info).length != 0 %}
对于一个对象/字典空测试
【讨论】:
array.length | array[0] 返回未定义。
注意,如果你想在数字类型的字段中区分一个未定义的值和一个零值,你需要这样做:
//this test will be true only on undefined values
{% if !field and field!==0 %} // note the double = !!. indeed in swig and in js !undefined and !0 are both true values
// this one will be true for undefined and 0 value fields
{% if !field %}
【讨论】:
{% if Object.length > 0 %}
{% endif %}
【讨论】: