文章来源:http://twig.sensiolabs.org/doc/tests/index.html
做逻辑判断的。目前支持的有
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
变量是否是奇数
- <PREclass=htmlname="code">{{ var is odd }}</PRE><BR>
- <BR>
- <P></P>
- <PRE></PRE>
- <P></P>
- <H1><Aname=t4></A>sameas</H1>
- <P>检查变量的地址是否相同</P>
- <P><PREclass=htmlname="code">{% if foo.attribute is sameas(false) %}
- the foo attribute really is the ``false`` PHP value
- {% endif %}</PRE><BR>
- <BR>
- <P></P>
- <H1><Aname=t5></A>constant</H1>
- <P>检查变量的值是否相同</P>
- <P><PREclass=htmlname="code">{% if post.status is constant('Post::PUBLISHED') %}
- the status attribute is exactly the same as Post::PUBLISHED
- {% endif %}</PRE><BR>
- <BR>
- <P></P>
- <H1><Aname=t6></A>defined</H1>
- <P>测试变量是否定义</P>
- <P><PREclass=htmlname="code">{# 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 %}</PRE><BR>
- <BR>
- <P></P>
- <H1><Aname=t7></A>empty</H1>
- <DIV>测试变量是否为空,empty的意思是:该变量已经定义,但他的值是 null false 或者是空字符串</DIV>
- <DIVsizcache="1"sizset="9"><PREclass=htmlname="code">{# evaluates to true if the foo variable is null, false, or the empty string #}
- {% if foo is empty %}
- ...
- {% endif %}</PRE><BR>
- <BR>
- </DIV>