如何在 symfony2 中使用 twig 继承更改标签模板

how to change a label template using twig inheritance in symfony2

在 Twig 中使用 Symfony2.3.​​4。

假设我正在尝试添加一个冒号 (:),如果 需要 ,则向表单中每个字段的每个标签添加一个星号 (*)由 Symfony2 的 CRUD 生成。为此,我使用一个树枝模板来继承 Symfony2 的主模板 form_div_layout.html.twig.

到目前为止:

//config.yml  
twig:
    form:
        resources:
        - ::my_form_layout.html.twig

//my_form_layout.html.twig
{% block form_label %}
    {% spaceless %}
        {% if label is not sameas(false) %}
            ...
            {% set asterisk = '<sup><i title="Campo obligatorio" 
            class="glyphicon-asterisk" style="color: red; font-size: 
            8px"></i></sup>' %}
            <label {% for attrname, attrvalue in label_attr %} 
            {{ attrname }}="{{ attrvalue }}"{% endfor %}>
            {{ label|trans({}, translation_domain) }}: {% if required %}
            {{ asterisk|raw }} {% endif %}
            </label>
            ...
        {% endif %}
    {% endspaceless %}
{% endblock form_label %}

当我呈现一个 choice-type 用于选择一个人的性别的字段时,问题是这样的,其中 expandedrequired 设置为 TRUE,冒号 (:) 和星号 (*) 出现在单词 Sex 旁边 AND 单词 MaleFemale

如何使模板区分 parent 和两个 children,使冒号和星号仅出现在 Sex 之后.

谢谢

这就是我做类似事情的方式。在自定义表单布局中稍微修改 'choice_widget_expanded' 块:

// update this row: {{ form_label(child) }}
{{ form_label(child, child, {'exclude_additions': 'true'}) }}

并更新您的 'form_label' 以检查是否定义了此值:

{{ label|trans({}, translation_domain) }}{% if exclude_additions is not defined %}: {% if required %}
{{ asterisk|raw }} {% endif %} {% endif %}