{% load suit_tags %}
{% with singlefield=line.fields|length_is:'1' %}
  <div class="control-group form-row{% if line.errors %} {{ singlefield|yesno:'error,first-error' }} errors{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %} {{ field.field.field.widget.attrs.rowclass }}{% endfor %}{{ singlefield|yesno:',multi-field-row' }}{% if not line.has_visible_field %} hidden{% endif %}">
    {% for field in line %}{#% spaceless %#}{% if not field.field.is_hidden %}
      {# write special control tags only for first multi field #}
      {% if forloop.first %}
        <div{% if not singlefield %}
               class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}"{% endif %}>

        <div class="controls">
          {# if multi-fields and first field #}
          {% if not singlefield %}
            <div class="multi-field-box">
          {% endif %}
      {% else %}
        {# If multi-fields and not first wrap also label #}
        {% if not singlefield and not forloop.first %}
          <div class="multi-field-box{{ field.errors|yesno:' error,' }}">
        {% endif %}
      {% endif %}

      {% if not field.is_readonly and field.errors %}<div class="inline error errors">{% endif %}

      {# if multi-fields and wrap controls too #}
      {% if not singlefield %}
        <div class="multi-field-controls">
      {% endif %}

      {% if field.is_readonly %}
        <span class="readonly">{{ field|field_contents_foreign_linked|linebreaksbr }}</span>
      {% else %}
        {{ field.field }}
      {% endif %}

      {# For single field errors#}
      {% if singlefield and line.errors %}
        <span class="help-inline">{{ line.errors }}</span>
      {% endif %}

      {# For multi field errors #}
      {% if field.errors and not singlefield and not field.is_readonly %}
        <span class="help-block">{{ field.errors|striptags }}</span>
      {% endif %}

      {% if field.field.help_text %}
        <span class="{% if line.errors or field.errors or not singlefield %}help-block{% else %}help-inline{% endif %}">{{ field.field.help_text|safe }}</span>
      {% endif %}

      {% if not field.is_readonly and field.errors %}</div>{% endif %}

      {% if not singlefield %}
        </div> {# close multi-field-controls #}
        </div> {# close multi-field-box #}
      {% endif %}

      {% if forloop.last %}
        </div>
        </div>
      {% endif %}
    {#% endspaceless %#}{% endif %}{% endfor %}
  </div>
{% endwith %}
