我有一个视图,我试图根据http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual覆盖单个字段的表单主题- 田.
视图看起来像这样:
{% form_theme form _self %}
{% block _my_form_foo_widget %}
<div class="input-append">
{{ block('number_widget') }}
<span class="add-on">%</span>
</div>
{% endblock %}
<form>
{{ form_row(form.foo) }}
{{ form_row(form.bar) }}
</form>
Run Code Online (Sandbox Code Playgroud)
对于foo和bar行,一切看起来都是预期的,但是_my_form_foo_widget块本身也包含在输出中,即:
<div class="input-append">
<span class="add-on">%</span>
</div>
<form>
<div>
<label for="my_form_foo">Bar</label>
<div class="input-append">
<input type="text" id="my_form_foo" name="my_form[foo]">
<span class="add-on">%</span>
</div>
</div>
<div>
<label for="my_form_bar">Foo</label>
<input type="text" id="my_form_bar" name="my_form[bar]">
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
我不能为我的生活弄清楚我做错了什么.作为一种解决方法,我只是在HTML注释中包含了块.
我在Symfony 2.4.1和Twig 1.15.0上.
您正在经历twig预期的行为.
如果您没有扩展另一个模板,则直接在当前模板中呈现新定义的块.
例:
template_A.html.twig:
<html>
<body>
{% block content -%}
Foo
{%- endblock -%}
Bar
{%- block more_content -%}
Foo
{%- endblock %}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
=>输出:( FooBarFoo正在渲染模板+正文中的所有块)
例:
template_B.html.twig:
template_A.html.twig{% extends 'templateA.html.twig' %}
{% block content -%}
Bar
{%- endblock %}
{% block not_in_template_a %}
Some String
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
=>输出:BarBarFoo (但不使Some String因为该块not_in_template_a未在原始模板呈现)