6

我正在尝试在已经扩展了一个模板的模板中自定义 form_label。

我在Symfony2 文档中使用示例:

{% use 'form_div_layout.html.twig' with form_label as base_form_label %}

{% block form_label %}
    {{ block('base_form_label') }}

    {% if required %}
        <span class="required" title="This field is required">*</span>
    {% endif %}
{% endblock %}

但没有任何改变!

你能帮助我吗?

4

1 回答 1

8

这是我的解决方案。

在我的 form.html.twig 文件顶部:

{% form_theme form with 'MyBundle:Activity:Form/fields.html.twig' %}

现在进入fields.html.twig,我自定义了form_label:

{% extends 'form_div_layout.html.twig' %}

{% block form_label %}
{% spaceless %}
    {% if not compound %}
        {% set label_attr = label_attr|merge({'for': id}) %}
    {% endif %}
    {% if required %}
        {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
    {% endif %}
    {% if label is empty %}
        {% set label = name|humanize %}
    {% endif %}

    <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}{% if attr.note is defined %} <span style="font: 11px normal; font-family: arial;">({{ attr.note }})</span>{% endif %}</label>
{% endspaceless %}
{% endblock form_label %}
于 2013-05-08T17:50:37.460 回答