Set simple boolean in twig

Viewed 17616

lets say i have some data like this :

answers: [
{
  answerText: "please",
  small: false
},
{
  answerText: "help",
  small: true
},
{
  answerText: "me",
  small: false
}
],

and i want to set a boolean that´s true if there is an answer where small is true. and i need to use it outside the loop i´m iterating over answers.

im trying arround and just dont get it, i think my nearest attempt is sth. like this

{% set zyx =  if 'small' in question['answers'] %}

{% set zyx =  'small' in question['answers'] %}

{% set zyx = 'small:true' in question['answers'] %}

{% set zyx = true in question['answers'] %}

but they all dont work as i expect

for any help thanks in advance

3 Answers

In my case,

for setting/initializing the variable value i wrote,

{% set status = false %}

and for checking

{% for item in items %}
    {% if status is not true %}
       // do something

       {% if item.something is true %}
          // do something

          {% set status = true %}

       {% elseif item.something is false %}
           // do something

           {% set status = false %}

       {% endif %}

    {% endif %}

  {% endfor %}
Related