内联表单 template/theme 在声明的位置渲染

Inline form template/theme rendering where declared

我有一个简单的表单,我需要一个字段的自定义模板来呈现 <input> 标记旁边的内容。因为我在其他任何地方都不需要这个,所以我想我应该把它放在与建议的表单相同的模板中 here:

{% form_theme form _self %}
{% block text_widget %}
  {{ block('form_widget_simple') }}
  something
{% endblock %}

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

这就是整个模板(与 ajax 一起使用,因此没有周围的标记)。

现在的问题是,"something" 在声明块 text_widget 的输出开始处呈现,就像任何其他块一样。它以 <input>:

旁边的形式呈现得很好
  something

<form name="form" method="post" action="">
<table id="form"><tr>
        <td>                <label for="form_Search" class="required">Search</label></td>
        <td>  <input type="text" id="form_Search" name="form[Search]" required="required" autofocus="autofocus" />
  something
</td>
    </tr><tr style="display: none">
        <td colspan="2"><input type="hidden" id="form__token" name="form[_token]" value="dUwdoiz9vo1TJTRjvyUcz9Rwd-D7pTvqUH-R0zCtg28" /></td>
    </tr></table>
</form>

这显然使内联主题完全无法使用,所以我想我可能做错了什么...

如何去掉开头多余的 "something"?

问题已经写好了,问题也解决了,不妨回答一下:

解决方案是从虚拟基础模板派生模板以吞并基础模板中定义的块之外的任何输出:

{# empty.html.twig #}
{% block content %}
{% endblock %}

而对于实际需要的模板:

{% extends 'empty.html.twig' %}
{% form_theme form _self %}
{% block text_widget %}
  {{ block('form_widget_simple') }}
  something
{% endblock %}

{% block content %}
  {{ form_start(form) }}
  {{ form_widget(form) }}
  {{ form_end(form) }}
{% endblock %}

在已经使用继承的常规模板中自定义字段时,人们可能不会三思而后行,但这种方式感觉就像是 hack...