做邏輯判斷的。目前支持的有
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
變量是否是奇數
<pre name="code" class="html">{{ var is odd }}
<pre name="code" class="html">{{ 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的專欄