Pergunta

In my Symfony2 project, I noticed that there are only two layout options for rendering forms:

form_table_layout.html.twig and form_div_layout.html.twig

These both reside in symfony\src\Symfony\Bridge\Twig\Resources\views\Form, and I would like to add one of my own somewhere in my app\Resources directory.

The only issue I'm having, is when I add my new layout file (form_list_layout.html.twig) into app\Resources\views\Form, Symfony doesn't look for it and just looks in the default location instead (in the Twig directory).

I have tweaked my config.yml to tell it to include the new layout, but maybe I'm doing something wrong:

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    form:
        resources: ['form_list_layout.html.twig']

EDIT: Okay, I've got Symfony2 looking in the right place now, but it doesn't seem to have any effect on the rendering of the form (I copied the contents of the table layout file into my new list layout file to test the theory, and my form is still rendering with <div> tags instead. This might be caused by the CraueFormFlowBundle I'm using, so I'll check it out).

The working config.yml:

 twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    form:
        resources:
            - ":Form:form_list_layout.html.twig"

EDIT 2: Doesn't look like its anything to do with the CraueFormFlowBundle, is there anything in my configuration that might be causing it to ignore the global form layout?

EDIT: Actually, it is working. As soon as I changed my layout file to include my list elements instead of the table layout content, it appeared okay.

Foi útil?

Solução

You don't have to place it into an app subfolder; you can place it in any bundle you want. For example, in my case, I've placed it into CommonBundle/Resources/views/Form/fields.html.twig and activate it in the config.yml file as

twig:
    form:
        resources: [ 'CommonBundle:Form:fields.html.twig' ]

Of course, if you put it into an app subfolder, you just omit the bundle name part:

twig:
    form:
        resources: [ ':Form:fields.html.twig' ]

Outras dicas

To correctly override a form layout or add a new one, place the file in your app/Resources/views/Form directory, and tweak your config.yml to something like this...

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    form:
        resources:
            - ":Form:form_list_layout.html.twig"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top