树枝表格 - 不要显示复选框

时间:2015-07-23 18:27:41

标签: symfony checkbox twig template-engine

这可能很简单,但我的表单主题中有一个标签和复选框覆盖块(以及许多其他块)。我希望所有标签都由form_label块处理,除了复选框'。

我目前正在将标签渲染包装在if语句中,我不想这样做,因为它没有感觉干净":

{%-  if form.vars.block_prefixes.1 is not defined or form.vars.block_prefixes.1 != 'checkbox' -%}

以下是我重写的块,我可以在checkbox_widget块中禁用标签吗?

{#
    ############# Checkbox #############
#}

{%- block checkbox_widget -%}
    {% spaceless %}
        <label  for="{{ id }}"><input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />{{ label|raw }}</label>
    {% endspaceless %}
{%- endblock checkbox_widget -%}

{#
############# Labels #############
#}

{%- block form_label -%}
    {% if label is not sameas(false) -%}
        {% set label_attr = label_attr|merge({'class': ('FormItem-label' ~ label_attr.class|default(''))|trim}) %}
        {% 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('') ~ ' is-required')|trim}) %}
        {%- endif %}
        {% if label is empty -%}
            {%- if label_format is not empty -%}
                {% set label = label_format|replace({
                    '%name%': name,
                    '%id%': id,
                }) %}
            {%- else -%}
                {% set label = name|humanize %}
            {%- endif -%}
        {%- endif -%}
        {%-  if form.vars.block_prefixes.1 is not defined or form.vars.block_prefixes.1 != 'checkbox' -%}
            <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
        {%- endif -%}
    {%- endif -%}
{%- endblock form_label -%}

{%- block button_label -%}{%- endblock -%}

2 个答案:

答案 0 :(得分:0)

手动将其添加到树枝模板中,以免弄乱表单主题。

{% block field_row %}
    <div class="Grid-cell">
        <div class="FormItem FormItem--checkbox is-required" required="required">
            <label for="terms_and_conditions">
                {{ form_widget(form.terms_and_conditions) }}
                {% autoescape false %}{{ form.terms_and_conditions.vars.label  }}{% endautoescape %}
            </label>
            {{ form_errors(form.terms_and_conditions) }}
        </div>
    </div>
{% endblock %}

答案 1 :(得分:0)

您应该使用:

{{ form_label(form.terms_and_conditions) }}

Documentation

相关问题