簡體   English   中英

Twig檢查數組中的值是否為true

[英]Twig check if value in array is true

我有一個包含布爾值的數組。 如何搜索數組以查看一個或多個是否為真,然后顯示<h1>一次?

到目前為止,這是我的代碼

{% set guides = 
              [
                 product.is_user_guide,
                 product.is_product_guide,
                 product.is_installation_guide
              ] 
              %}

               {% for guide in guides %}
                  {% if (guide) %}
                  <h1>There is a guide!</h1>
                  {% endif %}
              {% endfor %}

在上面的代碼中,它在數組中找到2個值為true,並顯示h1兩次。 如何修改它只顯示一次?

您可以in以下位置使用包含運算符

{% set guides = [
    product.is_user_guide,
    product.is_product_guide,
    product.is_installation_guide
] %}

{% if true in guides %}
   <h1>There is a guide!</h1>
{% endif %}

演示: http//twigfiddle.com/pf4xjp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM