Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic variables in Twigs

Tags:

twig

symfony

In a form, I dynamically define the fields in a controller. Then in the TWIG I would like to use form_widget to output the fields.

The normal approach, when knowing the fields is like shown bellow:

{{ form_widget(form.field1) }}

In this case, we cannot know whether we will have a field1, field2, etc. on beforehand. Though we have the field names in a variable called key.

So what we would like to achieve is replace the hardcoded field1 by something dynamic.

like image 812
Roel Veldhuizen Avatar asked Aug 07 '26 04:08

Roel Veldhuizen


1 Answers

You can do something like

{{ form_widget(form[key]) }}
like image 188
BENARD Patrick Avatar answered Aug 09 '26 19:08

BENARD Patrick