TWIG的 tests 学习_PHP教程

WBOY
发布: 2016-07-13 17:48:10
原创
997 人浏览过

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

divisibleby null even odd sameas constant defined empty

divisibleby
检查是否能被整除

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

null
{{ var is null }}
{{ var is null }}

even
变量是否是偶数{{ var is even }}
{{ var is even }}


odd
变量是否是奇数

{{ var is odd }} 
{{ var is odd }} 

sameas
检查变量的地址是否相同

{% if foo.attribute is sameas(false) %}
the foo attribute really is the ``false`` PHP value
{% endif %}
{% 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 %}
{% 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 %}
{# 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 %}

摘自 jiaochangyun的专栏

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478456.htmlTechArticle做逻辑判断的。目前支持的有 divisibleby null even odd sameas constant defined empty divisibleby 检查是否能被整除 {% if loop.index is divisibleby(3) %} ... {% en...
登录后复制
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!