Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding condition if in attr of form_widget

Tags:

twig

symfony

I'd like to display checked if a variable is null so I have make this code

{{ form_widget(form.showPrice, {% if travel is null %}  {'attr': {'checked': 'checked'}} {% endif %} ) }}

but I got this error

A hash key must be a quoted string, a number, a name, or an expression enclosed in
parentheses (unexpected token "operator" of value "%" in AppBundle:Dashboard/Travel:form.html.twig at line 100
like image 955
hous Avatar asked Feb 21 '15 15:02

hous


2 Answers

Delimiters like {% ... %} is used to execute statements such as for-loops.

Delimiters like {{ ... }} prints the result of an expression to the template.

You messed up with them. The right code would be:

{{ form_widget(form.showPrice, (travel is null ? {'attr': {'checked': 'checked'} }) ) }}
like image 108
Michael Sivolobov Avatar answered Nov 03 '22 07:11

Michael Sivolobov


Try this :

{{ form_widget(form.showPrice, (travel is null ? {'attr': {'checked': 'checked'} } : {}) ) }}

you need to also pass the false option - in this case {}

like image 3
Manse Avatar answered Nov 03 '22 06:11

Manse