覆盖树枝块:覆盖块的内容也作为文档的一部分呈现

时间:2016-02-23 18:20:02

标签: symfony twig

我正在阅读有关表单自定义的this部分。

现在我想自己定制一个小部件,所以我编写了下面的代码来覆盖{% block money_widget %}。它工作正常,但我的新块的内容也会显示在图像中。为什么呢?

{% use 'form_div_layout.html.twig' %}

{% form_theme form _self %}

{% block money_widget %}
here my the content of the block
{% endblock %}

{% block content %}
{{ form(form) }}
{% endblock %}

enter image description here

这是表单类型:

    $builder
        ->add('user', null, array('label' => 'Cliente'))
        ->add('numberPlate', null, array('label' => 'Número matrícula', 'data' => 'prueba'))
        ->add('subtotal', MoneyType::class)
        ->add('tax', MoneyType::class, array('label' => 'I.V.A.'))
        ->add('total', MoneyType::class, array('label' => 'Total'))
        ->add('Guardar', SubmitType::class)
    ;

编辑:我发现这个"问题"确实发生:当覆盖小部件的块位于使用include 包含的模板内时。但为什么?无论如何,为了不发生这种情况,我创建了一个作为表单主题的模板,并以这种方式添加它,例如:

{% form_theme form 'here_is_the_block_to_override_the_widget.html.twig' %}

在我使用include

的模板中

2 个答案:

答案 0 :(得分:0)

form_div_layout中的原始小部件:

{%- block money_widget -%}
    {{ money_pattern|replace({ '{{ widget }}': block('form_widget_simple') })|raw }}
{%- endblock money_widget -%}

你的区块:

{% block money_widget %}
    here my the content of the block
{% endblock %}

当您在自己的模板中覆盖原始块时,只要需要渲染Money窗口小部件,就会显示模板中的块。因此,在您的最后一页中,您会看到这里我的块内容,正如预期的那样。顺便说一句,在您的教程页面中没有use语句,这可能会让人感到困惑。

答案 1 :(得分:0)

因为您的模板必须扩展引用base的<{1}}模板。

使用以下内容在block content中创建最小基本模板

app/Ressources/views/base.html.twig

并将其扩展到您的模板中,如下所示:

{% block content %}                
{% endblock content %}    
相关问题