Symfony 2 - Form theming - Overriden block renders two times

我只是一个虾纸丫 提交于 2020-01-17 06:26:08

问题


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


回答1:


Solved using separated template file like is suggested in documentation.

http://symfony.com/doc/current/cookbook/form/form_customization.html#method-2-inside-a-separate-template




回答2:


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.

submit_widget looks like:

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

And this block contains 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:

{% 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 %}


来源:https://stackoverflow.com/questions/27720989/symfony-2-form-theming-overriden-block-renders-two-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!