{% macro render_field_standard(form, field, classes) -%}
{% set label_att={'class': 'control-label ' + classes.label} -%}
{% if field.auto_id -%}
    {% do label_att.update({'for': field.auto_id}) -%}
{% endif -%}
{% if field.field.required -%}
    {% do add_css_classes_to_dict(label_att, 'required') -%}
{% endif -%}
<label{{ flatatt(label_att) }}>{{ field.label }}</label>

<div class="field {{ classes.field }}">
    {{ field|safe }}
    {% for error in field.errors -%}
    <div class="alert alert-warning {{ form.error_css_class }}">{{ error }}</div>
    {% endfor -%}
    {% if field.help_text -%}
    <p class="help-block">
        {{ field.help_text|safe }}
    </p>
    {% endif -%}
</div>
{% endmacro -%}

{% macro render_field_displaytext(form, field, classes) -%}
<tr>
    {% set label_att = {'class': classes.label + ' success'} -%}
    {% if field.auto_id -%}
        {% do label_att.update({'for': field.auto_id}) -%}
    {% endif -%}
    {% if field.field.required -%}
        {% do add_css_classes_to_dict(label_att, 'required') -%}
    {% endif -%}
    <td{{ flatatt(label_att) }}>{{ field.label }}</td>

    <td class="info {{ classes.field }}">
        {{ field|safe }}
        {% for error in field.errors -%}
        <div class="alert alert-warning {{ form.error_css_class }}">{{ error }}</div>
        {% endfor -%}
        {% if field.help_text -%}
        <p class="help-block">
            {{ field.help_text|safe }}
        </p>
        {% endif -%}
    </td>
</tr>
{% endmacro -%}

{% macro render_field_checkbox(form, field, classes) -%}
<div class="{{ classes.label }}"></div>
<div class="field {{ classes.field }}">
    <div class="checkbox">
        {% set label_att = {'class': 'control-label'} -%}
        {% if field.auto_id -%}
            {% do label_att.update({'for': field.auto_id}) -%}
        {% endif -%}
        {% if field.field.required -%}
            {% do add_css_classes_to_dict(label_att, 'required') -%}
        {% endif -%}
        <label{{ flatatt(label_att) }}>
            {{ field|safe }} <span>{{ field.label }}</span>
        </label>
        {% for error in field.errors -%}
        <div class="alert alert-warning {{ form.error_css_class }}">{{ error }}</div>
        {% endfor -%}
        {% if field.help_text -%}
        <p class="help-block">
            {{ field.help_text|safe }}
        </p>
        {% endif -%}
    </div>
</div>
{% endmacro -%}

{% macro render_field_multiple(form, field, classes) -%}
{% set label_att = {'class': 'control-label ' + classes.label} -%}
{% set field_att = {} -%}
{% if field.auto_id -%}
    {% do label_att.update({'for': field.auto_id}) -%}
    {% do field_att.update({'id': field.auto_id}) -%}
{% endif -%}
{% if field.field.required -%}
    {% do add_css_classes_to_dict(label_att, 'required') -%}
{% endif -%}
<label{{ flatatt(label_att) }}>{{ field.label }}</label>
<div class="field {{ classes.field }}">
    <div{{ flatatt(field_att) }}></div>
    <div class="input-as-select">
        {% for choice in field -%}
        <div class="{{ classes.multiple_type }}">
            <label>
                {{ choice.tag()|safe }}
                {{ choice.choice_label }}
            </label>
        </div>
        {% endfor -%}
        {% for error in field.errors -%}
        <div class="alert alert-warning {{ form.error_css_class }}">{{ error }}</div>
        {% endfor -%}
        {% if field.help_text -%}
        <p class="help-block">
            {{ field.help_text|safe }}
        </p>
        {% endif -%}
    </div>
</div>
{% endmacro -%}

{% macro bs_field(field, classes={}) -%}
{% set def_classes = {'label': 'col-md-2', 'field': 'col-md-6', 'multiple_type': ''} -%}
{% do def_classes.update(classes) -%}
{% if field|get_displaytext_layout == 'table' -%}
    {{ render_field_displaytext(form, field, def_classes) }}
{% else -%}
<div class="row form-group{% if field.errors -%} has-error{% endif -%}">
    {% if field|get_displaytext_layout == 'div' -%}
        {{ render_field_standard(form, field, def_classes) }}
    {% elif field|is_checkbox -%}
        {{ render_field_checkbox(form, field, def_classes) }}
    {% elif field|is_multiple_checkbox -%}
        {% do classes.update({'multiple_type': 'checkbox'}) -%}
        {{ render_field_multiple(form, field, def_classes) }}
    {% elif field|is_radio -%}
        {% do classes.update({'multiple_type': 'radio'}) -%}
        {{ render_field_multiple(form, field, def_classes) }}
    {% else -%}
        {{ render_field_standard(form, field, def_classes) }}
    {% endif -%}
</div>
{% endif -%}
{% endmacro -%}
