I want to customize form rendering in Symfony. I checked the docs and found out that its possible to set the theme for rendering with the {{ form_theme }}
tag. This works perfectly if only one form is present at the time but if you have multiple forms in the same template it doesn’t work.
A simple example from my index.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<h1>Hello Form 1</h1>
{% form_theme form1 'bootstrap_3_layout.html.twig' %}
{{ form_start( form1 ) }}
{{ form_end( form1 ) }}
<h1>Hello Form 2</h1>
{% form_theme form2 'bootstrap_3_horizontal_layout.html.twig' %}
{{ form_start( form2 ) }}
{{ form_end( form2 ) }}
{% endblock %}
As you can see there are two form variables and I want form1
to be rendered with 'bootstrap_3_layout.html.twig'
and form2
with 'bootstrap_3_horizontal_layout.html.twig'
. I don't know the Twig internals but I think the themes override their block definitions.
The result looks like this
So my questions is how can I render the forms with the given themes not interfering each other. Is there any way to render a form in a separate clean twig process?
I tried a Twig Extension with a custom function but it seems the function is using the same Twig_Environment. I also tried a sub request with {% render }% but that doesn't work either.