简体   繁体   English

Symfony 2-表单主题-覆盖块渲染两次

[英]Symfony 2 - Form theming - Overriden block renders two times

I'm trying to customize a theme form submit button but this button is being rendered two times. 我正在尝试自定义主题表单提交按钮,但是此按钮被渲染了两次。 The first time when the block is overriden and the second time when i'm using it in my form. 第一次是覆盖该块,第二次是在表单中使用它。

Here is the code and the result. 这是代码和结果。

Thanks a lot. 非常感谢。

{# 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) }}

Link to the image (don't have 10 reputation ¬¬) Double rendered button image 链接到图像(没有10点信誉¬¬)两次呈现的按钮图像

If you look at the https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig , then you can see, that {% block submit_widget %} doesn't contain button element. 如果您查看https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig ,那么您会看到{%阻止Submit_widget%}不包含按钮元素。

submit_widget looks like: Submit_widget如下所示:

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

And this block contains button_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 %}

You must change button_widget (and add class fa fa-bicycle ) if you want to add font awesome icon - like: 如果要添加字体很棒的图标,则必须更改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 %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM