2016-08-23 17 views
2

的給定一個PHP數組中的一個樹枝模板:查找,如果樹枝數組是空的非保護值

object(Drupal\Core\Template\Attribute)#1208 (1) { 
    ["storage":protected]=> array(0) { } 
} 

我如何檢查是否有數組中沒有非保護的元素呢?我的想法是,我只能操作非保護值,所以如果只有受保護值存在,我可以假裝數組爲空。

到目前爲止,我的檢查如下:

{% if attributes is defined and attributes is not empty %} 
    <div{{ attributes }}> 
     {{ content }} 
    </div> 
{% else %} 
    {{ content }} 
{% endif %} 

在其目前的形式,這將顯示<div>[Content]</div>。相反,我想看到:[Content]

任何幫助嗎?

回答

0
  1. 擴展枝杈

    <?php 
        $twig = new Twig_Environment($loader); 
        $twig->addFilter(new Twig_SimpleFilter('accessible_properties', 'get_object_vars')); 
    
  2. 使用它裏面的模板

    {% set public_attributes = attributes is defined ? (attributes|accessible_properties) : [] %} 
    {% if public_attributes is not empty %} 
        ... 
    {% else %} 
        ... 
    {% endif %}