<!-- Система форм -->

{%- macro render_form(form, action=None, name="default") -%}
  {# XXX: Place for global form error.
    {% if form.errors %}...{% endif %}
  #}
  {% for name, field in form.form(name) %}
  {{ row(field, name, **kwargs) }}
  {% endfor %}
  <input type="hidden" value="1" name="form.submitted">
  {% if action %}
    <div class="form-actions">
      <button type="submit" class="btn btn-primary">{{ action }}</button>
    </div>
  {% endif %}
{%- endmacro %}

{%- macro row(field, name) %}
<div class="control-group {% if field.errors %}error{% endif %}">
  <label for="{{field.name}}">{{ field.label }}</label>
  <div class="controls">
    {# <p>{{field | f(name=name)}}</p> #}
    {% if field.errors %}<span class="help-inline">{{ field.errors|join(', ') }}</span>{% endif %}
    {% if field.description %}<span class="help-block">{{ field.description }}</span>{% endif %}
  </div>
</div>
{%- endmacro %}

{%- macro render_search(form, url="/", action="Найти") %}
  <form class="well form-inline" action="{{ url }}" method="GET">
    {% for name, field in form %}
      {{ field(class="input-small", placeholder=name, **kwargs)|safe }}
    {% endfor %}
    <button class="btn btn-primary pull-right" type="submit" class="btn">{{ action }}</button>
  </form>
{%- endmacro %}

{%- macro add_menu(items) %}
  <p>
    {%- for item in items %}
      {% if item.has_key('active') and item['active'] %}
        <a href="{{ item.link }}" class="btn btn-info btn-small"><i class="icon-plus icon-white"></i>Добавить {{ item.title }}</a>
      {% endif %}
    {%- endfor %}
  </p>

{%- endmacro %}


{%- macro render_table(form, me, model, items, actions=[{'name': 'save','title':'Save'},]) %}
<table class="table table-striped table-bordered table-condensed">
  <thead>
  <tr>
    <th>
      <input type="checkbox" name="ids" value="" checked="">
    </th>
    <!-- <th>&nbsp;</th> -->
    {% for name, field in form %}
    <th>{{ name }}</th>
    {% endfor %}

  </tr>
  </thead>
  <tbody>
  {% for item in items %}
  <tr>
    <td><input type="checkbox" name="ids" value="{{ item.key() }}"></td>
    <!-- <td>
      <div state="view" class="state_switch">
        <a class="view do_switch" to_state="edit"><i class="icon-pencil"></i></a>
        <a class="edit do_switch" to_state="view"><i class="icon-remove"></i></a>
      </div>
    </td> -->
    {% for name, field in form %}
    <td class="view"><a href="{{ url_for(me.one, model=model._name, key=item.key()) }}">
{{ item[name] }}</a></td>
    {% endfor %}
  
  </tr>
  {% else %}
    <!-- Если пусто, то выводить это -->

    <tr>
      <td colspan="{{ form|length+2 }}">
        <h3>Результатов не найдено.</h3>
      </td>
    </tr>
    <!-- /если пусто... -->

  {% endfor %}
  </tbody>
</table>

<div class="well">
  {% for action in actions %}
    <button type="submit" name="{{ action.name }}" class="btn btn-danger" value="{{ action.name }}">{{ action.title }}</button>&nbsp;
  {% endfor %}
</div>

{%- endmacro %}