I've got a view where I'm trying to override the form theme for an individual field per http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field.
The view looks something like this:
{% 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>
Everything looks as expected for the foo and bar rows, however, the _my_form_foo_widget
block itself is also included in the output, i.e.:
<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>
I can't for the life of my figure out what I'm doing wrong. As a workaround I just wrapped the block in HTML comments.
I'm on Symfony 2.4.1 and Twig 1.15.0.
You are experiencing twig
's expected behaviour.
A newly defined block is being rendered directly in the current template if you're not extending another template.
Example:
template_A.html.twig
:
<html>
<body>
{% block content -%}
Foo
{%- endblock -%}
Bar
{%- block more_content -%}
Foo
{%- endblock %}
</body>
</html>
=> ouputs: FooBarFoo
( all blocks present in the template + body is being rendered )
Example:
template_B.html.twig
:
template_A.html.twig
{% extends 'templateA.html.twig' %}
{% block content -%}
Bar
{%- endblock %}
{% block not_in_template_a %}
Some String
{% endblock %}
=> outputs: BarBarFoo
(but does not render Some String
because the block not_in_template_a
was not present in the original template)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With