扩展bootstrap_3_layout.html.twig表单主题

时间:2018-07-27 10:21:57

标签: forms symfony twig

我正在尝试通过扩展bootstrap_3_layout.html.twig表单主题来自定义symfony 3项目中的表单布局。

{# app/Resources/views/app/custom_bootstrap_metronic.html.twig #}
{% use "bootstrap_3_layout.html.twig" %}

{% block checkbox_widget -%}
{%- set parent_label_class = 
parent_label_class|default(label_attr.class|default('')) -%}
    {% if 'checkbox-inline' in parent_label_class %}
        {{- form_label(form, null, { widget: parent() }) -}}
    {% else -%}
        <div class="checkbox">
            {{- form_label(form, null, { widget: parent() }) -}}
        </div>
    {%- endif %}
{%- endblock checkbox_widget %}

我将其添加到配置文件中:

{# app/config/config.yml #}
twig:
   # form_themes: [bootstrap_3_layout.html.twig]
   form_themes: [form/custom_bootstrap_metronic.html.twig]

我要覆盖的是checkbox_widget,因此呈现的html会像这样:

<div class="form-group">
    <div class="checkbox">
        <label class="mt-checkbox">
            <input type="checkbox" id="appbundle_accept" 
            name="appbundle[accept]" value="1"> 
            Label
            <span></span>
        </label>
    </div>
</div>

代替:

<div class="form-group">
    <div class="checkbox">
        <label>
            <input type="checkbox" id="appbundle_accept" 
            name="appbundle[accept]" value="1"> 
            Label
        </label>
    </div>
</div>

我不能这样做,因为parent()函数从bootstrap_3_layout.html.twig返回checkbox_widget块,我需要从form_div_layout.html.twig返回checkbox_widget

1 个答案:

答案 0 :(得分:2)

我所做的是将代码从“ bootstrap_3_layout.html.twig”复制到名为“ custom_bootstrap_metronic.html.twig”的文件中,然后我对其进行了自定义。

{% block checkbox_widget -%}
    {%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
    {% if 'checkbox-inline' in parent_label_class %}
        {{- form_label(form, null, { widget: parent(), label_attr: {class: 'mt-checkbox'}}) -}}
    {% else -%}
        <div class="mt-checkbox-inline">
            {{- form_label(form, null, { widget: parent(), label_attr: {class: 'mt-checkbox'}}) -}}
        </div>
    {%- endif %}
{%- endblock checkbox_widget %}

{% block checkbox_radio_label %}
    {# Do not display the label if widget is not defined in order to prevent double label rendering #}
    {% if widget is defined %}
        {% if required %}
            {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) %}
        {% endif %}
        {% if parent_label_class is defined %}
            {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ parent_label_class)|trim}) %}
        {% endif %}
        {% if label is not same as(false) and 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 %}
        <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
            {{- widget|raw }} 
            {{ label is not same as(false) ? (translation_domain is same as(false) ? label : label|trans({}, translation_domain)) -}}
            <span></span> {# added this line #}
        </label>
    {% endif %}
{% endblock checkbox_radio_label %}