2

I have an array which contains booleans. How do I search the array to see if one or more is true and then display the <h1> something once?

Here is my code so far

{% 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 %}

In the above code it finds 2 values in the array to true and displays the h1 twice. How can I modify it so it only displays once?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
ServerSideSkittles
  • 2,713
  • 10
  • 34
  • 60
  • You need to work with a [flag](http://stackoverflow.com/questions/39156701/twig-check-multiple-values/39159539#39159539) or write the logic inside your controller or extend twig with a function – DarkBee Sep 12 '16 at 12:41
  • Maybe solution is http://stackoverflow.com/questions/21672796/how-can-i-use-break-or-continue-within-for-loop-in-twig-template – aslawin Sep 12 '16 at 12:42

1 Answers1

4

You can use the containment operator 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 %}

Demo: http://twigfiddle.com/pf4xjp

Yoshi
  • 54,081
  • 14
  • 89
  • 103