DoppyNL DoppyNL - 1 month ago 12
Twig Question

symfony2 twig form theme for different subsites

I've got a symfony2 application with multiple styles. All 3 have their own html + css for rendering.
Something like "frontsite", "user dashboard" and "admin backend".

Now there is a difference in form theme to be used in all 3 subsites, so there is a (slightly) different theme for all of them.

Because of this I can set the theme in the symfony configuration, but for 2 of the 3 subsites it will be wrong.

I can set a theme manually using:

{% form_theme form 'MyUberCoolBundle:Form:theme.html.twig' %}


But I don't really want to do that for every form.
I could set that in a base-template, but then my form would always need to be called "form".

Is there a way to set the theme to be used in a base template so it will be used for all forms?

Answer

After looking around for an answer I came to the conclusion it is not possible to do this only in configuration.

We resorted to the following solution

in config.yml we configured a global twig variable:

twig:
    globals:
        frontend_form_theme:          'MyUberCoolBundle:Form:theme.html.twig'

Then we set the correct theme on the form itself just prior to rendering using the global variable. This even allows us to use something different when desired.

{% form_theme form frontend_form_theme %}
{{ form_start(form) }}