Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set simple boolean in twig

Tags:

twig

symfony

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

like image 990
john Smith Avatar asked Dec 15 '13 15:12

john Smith


1 Answers

You cannot use statements in an expression. Removing if will do the trick:

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

EDIT

To check if answer.small is true, use:

{% set zyx = question['answers']['small'] is true %}
like image 175
Wouter J Avatar answered Sep 28 '22 02:09

Wouter J