TWIG的 tests 学习

文章来源:http://twig.sensiolabs.org/doc/tests/index.html

做逻辑判断的。目前支持的有

divisibleby null even odd sameas constant defined empty


divisibleby

检查是否能被整除

{% if loop.index is divisibleby(3) %}
    ...
{% endif %}


null

{{ var is null }}


even

变量是否是偶数

{{ var is even }}

odd

变量是否是奇数

{{ var is odd }}



sameas

检查变量的地址是否相同

{% if foo.attribute is sameas(false) %}
    the foo attribute really is the ``false`` PHP value
{% endif %}


constant

检查变量的值是否相同

{% if post.status is constant('Post::PUBLISHED') %}
    the status attribute is exactly the same as Post::PUBLISHED
{% endif %}


defined

测试变量是否定义

{# defined works with variable names #}
{% if foo is defined %}
    ...
{% endif %}

{# and attributes on variables names #}
{% if foo.bar is defined %}
    ...
{% endif %}

{% if foo['bar'] is defined %}
    ...
{% endif %}


empty

测试变量是否为空,empty的意思是:该变量已经定义,但他的值是 null false 或者是空字符串
{# evaluates to true if the foo variable is null, false, or the empty string #}
{% if foo is empty %}
    ...
{% endif %}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值