{% import 'jqueryuibootstrap_wtf.html' as wtf %}

{% macro render_form(form, cancel_url, extra=None) -%}
    <form action="" method="POST" class="form-horizontal"{% if form.has_file_field %} enctype="multipart/form-data"{% endif %}>
      <fieldset>
        {{ form.hidden_tag() }}

        {% for f in form if f.name != 'csrf_token' and f.name != 'csrf' %}
        <div class="control-group{% if f.errors %} error{% endif %}">
          {{ f.label(class='control-label') }}
          <div class="controls">
            <div>
              {% if not focus_set %}
              {{ f(autofocus='autofocus') }}
              {% set focus_set = True %}
              {% else %}
              {{ f() }}
              {% endif %}
            </div>
            {% if f.errors %}
              <ul>
              {% for e in f.errors %}
                <li>{{ e }}</li>
              {% endfor %}
              </ul>
            {% endif %}
          </div>
        </div>
        {% endfor %}
        <div class="control-group">
          <div class="controls">
            <input type="submit" class="btn btn-primary btn-large" value="{{ gettext('Submit') }}" />
            {% if extra %}
            {{ extra }}
            {% endif %}
            {% if cancel_url %}
            <a href="{{ cancel_url }}" class="btn btn-large">{{ gettext('Cancel') }}</a>
            {% endif %}
          </div>
        </div>
      </fieldset>
    </form>
{%- endmacro %}

{% macro generate_elements(form, display_blocks) -%}
    {{ form.hidden_tag() if form.hidden_tag }}
    {{ wtf.form_errors(form, False) }}
    {% for display_element in display_blocks %}
        {% if display_element['legend'] %}<legend>{{ display_element['legend'] }}</legend>{% endif %}
        {% for field in display_element['display'] %}
        {{ wtf.horizontal_field(form[field]) }}
        {% endfor %}
    {% endfor %}
{%- endmacro %}