【问题标题】:Symfony 2 - Form theming - Overriden block renders two timesSymfony 2 - 表单主题 - 覆盖块渲染两次
【发布时间】:2014-12-31 12:14:03
【问题描述】:

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

这是代码和结果。

非常感谢。

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

【问题讨论】:

  • 因为您没有扩展父模板,所以所有块都按原样呈现。这意味着您的 {% block submit_widget %}...{% endblock submit_widget %} 正在被渲染,然后您的表单带有来自您的 submit_widget 块的主题版本。
  • 此代码来自嵌入式控制器。 symfony.com/doc/current/book/… 在这种情况下我能做什么?我已经没有想法了。我想我不会为这个提交按钮设置主题,但这很遗憾。

标签: php symfony twig theming


【解决方案1】:

【讨论】:

  • 是的,这里有些问题,有些解决方案。问:为什么会这样?有什么区别?
【解决方案2】:

如果您查看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 %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2015-05-16
    相关资源
    最近更新 更多