[英]Find if twig array is empty of non-protected values
在twig模板中給出一個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]
有幫助嗎?
伸展樹枝
<?php $twig = new Twig_Environment($loader); $twig->addFilter(new Twig_SimpleFilter('accessible_properties', 'get_object_vars'));
在模板中使用它
{% set public_attributes = attributes is defined ? (attributes|accessible_properties) : [] %} {% if public_attributes is not empty %} ... {% else %} ... {% endif %}
如果這是在Drupal 8中,你可以通過render傳遞屬性值來查找,如下所示:
{% if attributes|render %}
<div{{ attributes }}>
{{ content }}
</div>
{% else %}
{{ content }}
{% endif %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.