Symfony 2 - 表单主题 - Overriden块呈现两次

时间:2014-12-31 12:14:04

标签: php symfony twig theming

我正在尝试自定义主题表单提交按钮,但此按钮正在呈现两次。第一次覆盖块时,第二次在我的表单中使用它时。

这是代码和结果。

非常感谢。

{# src/YagoQuinoy/Simple/BlogBundle/Resources/views/Blog/searchArtciles.html.twig #}

{% form_theme form _self %}

{% block submit_widget %}
    <button><i class="fa fa-bicycle"></i></button>
{% endblock submit_widget %}

{{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}
{{ form_errors(form.search) }}
{{ form_widget(form.search) }}
{{ form_widget(form.submit, {'attr': {'class': 'e-search-articles-submit'}}) }}
{{ form_end(form) }}

链接到图片(没有10个声誉¬¬)Double rendered button image

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

如果查看https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig,则可以看到{%block submit_widget%}不包含按钮元素。

submit_widget看起来像:

{% block submit_widget -%}
    {% set type = type|default('submit') %}
    {{- block('button_widget') -}}
{%- endblock submit_widget %}

此块包含button_widget:

{% block button_widget -%}
    {% if label is empty -%}
        {%- if label_format is not empty -%}
            {% set label = label_format|replace({
                '%name%': name,
                '%id%': id,
            }) %}
        {%- else -%}
            {% set label = name|humanize %}
        {%- endif -%}
    {%- endif -%}
    <button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ label|trans({}, translation_domain) }}</button>
{%- endblock button_widget %}

如果你想添加字体真棒图标,你必须更改button_widget(并添加类 fa fa-bicycle ),如:

{% block button_widget -%}
    {% if label is empty -%}
        {%- if label_format is not empty -%}
            {% set label = label_format|replace({
                '%name%': name,
                '%id%': id,
            }) %}
        {%- else -%}
            {% set label = name|humanize %}
        {%- endif -%}
    {%- endif -%}
    <button type="{{ type|default('button') }}" {{ block('button_attributes') }}><i class="fa fa-bicycle"></i>{{ label|trans({}, translation_domain) }}</button>
{%- endblock button_widget %}