程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> TWIG的 tests 學習

TWIG的 tests 學習

編輯:關於PHP編程

做邏輯判斷的。目前支持的有

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的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved