{% load form_tags %}

{% if field.is_hidden %}
    {{ field }}
{% else %}
    {% comment %}
        Make the field widget type available to templates so we can mark-up
        checkboxes differently to other widgets.
    {% endcomment %}
    {% annotate_form_field field %}

    {% block control_group %}
        <div class="control-group {% if field.errors %}error{% endif %}">

            {% block label %}
                {% if not nolabel and field.widget_type != 'CheckboxInput' %}
                    <label for="{{ field.auto_id }}" class="control-label{% if field.field.required %} required{% endif %}">
                        {{ field.label|safe }}
                        {% if field.field.required %} <span>*</span>{% endif %}
                    </label>
                {% endif %}
            {% endblock %}

            {% block controls %}
                <div class="controls">
                    {% block widget %}
                        {% if field.widget_type == 'CheckboxInput' %}
                            <label for="{{ field.auto_id }}" class="checkbox {% if field.field.required %}required{% endif %}">
                                {{ field.label|safe }}{% if field.field.required %} <span>*</span>{% endif %}
                                {{ field }}
                            </label>
                        {% else %}
                            {{ field }}
                        {% endif %}
                    {% endblock %}

                    {% block errors %}
                        {% for error in field.errors %}
                            <span class="error-block"><i class="icon-exclamation-sign"></i> {{ error }}</span>
                        {% endfor %}
                    {% endblock %}

                    {% block help_text %}
                        {% if field.help_text %}
                            <span class='help-block'>
                                {# We allow HTML within form help fields #}
                                {{ field.help_text|safe }}
                            </span>
                        {% endif %}
                    {% endblock %}
                </div>
            {% endblock %}
        </div>
    {% endblock %}
{% endif %}
