Twig form_theme _self customizing individual field
symfony, twig
Solution
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`:
- has a body (= code outside of blocks) => blocks rendered directly
<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`:
- extending template is not allowed to have a body
- will only render blocks that are present in `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)
Problem
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.