如何覆盖"form_div_layout"基本模板?

Ran*_*ngh 5 php forms symfony

我想覆盖要在twig中使用的基本tempale.

我用过这个

twig:
    form:
        resources:
            - 'form_div_layout.html.twig'
Run Code Online (Sandbox Code Playgroud)

我已将文件从原始位置复制到 app/resources/views/Form/form_div_layout.html.twig但仍然无法看到模板渲染中的更改.

基本上我只想在DIV框中添加类生成

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

我需要做出更多改变吗?

Car*_*dos 9

如果您只想自定义一个字段,则无需复制完整文件.做这个:

如果您只想为一个模板执行此操作,请将其添加到模板中:

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

如果要在多个模板中自定义此功能,请执行以下操作:使用以下代码在捆绑包中创建模板文件:

{# 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 %}
Run Code Online (Sandbox Code Playgroud)

然后,在要使用此自定义字段的模板中,执行以下操作:

{% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}
Run Code Online (Sandbox Code Playgroud)

如果您希望此自定义在捆绑包的所有模板中可用,请将其添加到配置文件中:

# app/config/config.yml
twig:
    form:
        resources:
            - 'AcmeDemoBundle:Form:fields.html.twig'
Run Code Online (Sandbox Code Playgroud)

如果要将其用于所有捆绑包,请将此文件复制到

app/Resources/AcmeDemoBundle/views/Form/fields.html.twig
Run Code Online (Sandbox Code Playgroud)