Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the "form_div_layout" base template?

Tags:

forms

php

symfony

I want to override the base tempale to be used in twig.

I have used this

twig:
    form:
        resources:
            - 'form_div_layout.html.twig'

and I have copied the file from original location to app/resources/views/Form/form_div_layout.html.twig but still I am not able to see the changes in template rendering.

Basically I just want to add the class in DIV box generated

{% block form_widget_compound %}
{% spaceless %}
    <div class="MYCLASS" 
    {{ block('widget_container_attributes') }}>
        {% if form.parent is empty %}
            {{ form_errors(form) }}
        {% endif %}
        {{ block('form_rows') }}
        {{ form_rest(form) }}
    </div>
{% endspaceless %}
{% endblock form_widget_compound %}

Do I need to make any more change ?

like image 643
Mirage Avatar asked Sep 03 '12 06:09

Mirage


2 Answers

You don´t need to copy the full file if you just want to customize one field. Do this:

If you just want to do this for just one template, add this to your template:

{% form_theme form _self %}

{% block form_widget_compound %}
{% spaceless %}
    <div class="MYCLASS" >
    {{ block('widget_container_attributes') }}>
        {% if form.parent is empty %}
            {{ form_errors(form) }}
        {% endif %}
        {{ block('form_rows') }}
        {{ form_rest(form) }}
    </div>
{% endspaceless %}
{% endblock form_widget_compound %}

If you want to customize this in several templates, do this: create a template file in your bundle with this code:

{# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #}
{% block form_widget_compound %}
{% spaceless %}
    <div class="MYCLASS" >
    {{ block('widget_container_attributes') }}>
        {% if form.parent is empty %}
            {{ form_errors(form) }}
        {% endif %}
        {{ block('form_rows') }}
        {{ form_rest(form) }}
    </div>
{% endspaceless %}
{% endblock form_widget_compound %}

Then in those templates where you want to use this customized field, do:

{% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}

If you want this customization to be available in ALL templates of a bundle, add this to your config file:

# app/config/config.yml
twig:
    form:
        resources:
            - 'AcmeDemoBundle:Form:fields.html.twig'

If you want this to be available for ALL bundles, copy this file to

app/Resources/AcmeDemoBundle/views/Form/fields.html.twig
like image 85
Carlos Granados Avatar answered Nov 07 '22 19:11

Carlos Granados


To do what you are trying copy just form_div_layout.html.twig in resources without the Form folder and then in config reffer to it this way: ::form_div_layout.html.twig

twig:
    form:
        resources:
            - '::form_div_layout.html.twig'

You don't have to copy all of it. You can extend only parts of it..

like image 1
Mihai Stancioiu Avatar answered Nov 07 '22 18:11

Mihai Stancioiu