{% macro render_field(field, class_="", show_label=True, input_prepend=None) %}
<div class="control-group {% if field.errors %}error{% endif %}">
  {% if show_label %}
  <label class="control-label" for="{{ field.name }}">{{ field.label.text }}
    {% if field.flags.required %}<span>*</span>{% endif %}:
  </label>
  {% endif %}
  <div class="controls">
    {% if input_prepend %}
    <div class="input-prepend">
    <span class="add-on"><i class="{{ input_prepend }}"></i></span>
    {% endif %}
    {% if field.flags.required %}
      {% set class_ = class_ + " required" %}
    {% endif %}
    {% if field.flags.email %}
      {% set class_ = class_ + " email" %}
    {% endif %}
    {{ field(class_=class_, **kwargs) }}
    {% if input_prepend %}
    {# close the input-prepend div if one exists #}
    </div>
    {% endif %}
    {% if field.description %}
    <br/><span class="help-inline"><small>{{ field.description }}</small></span>
    {% endif %}
    {% for error in field.errors %}
    <br/><span class="help-inline"><small>{{ error|string }}</small></span>
    {% endfor %}
  </div>
</div>
{% endmacro %}


{% macro render_month_year_date(month_field, year_field, label, class_="") %}
<div class="control-group {% if month_field.errors %}error{% endif %}">
  <label class="control-label" for="{{ month_field.name }}">
    {{ label }}
    {% if month_field.flags.required or year_field.flags.required %}*{% endif %}:
  </label>
  <div class="controls">
    {% for field in (month_field, year_field) %}
    {% if field.flags.required %}
      {% set class_ = class_ + " required" %}
    {% endif %}
    {% if field.flags.email %}
      {% set class_ = class_ + " email" %}
    {% endif %}
    {{ field(class_=class_ + ' input-small ', **kwargs) }}
    {% endfor %}
  </div>
</div>

{% endmacro %}


{% macro render_pagination(pagination, uri, endpoint) %}
<div class="pagination pagination-right">
  <ul>
    {% if pagination.has_prev -%}
    <li>
      <a href="{{ url_for(endpoint, uri=uri, page=pagination.prev_num, **kwargs) }}">
        &laquo; {% trans %}Previous{% endtrans %}
      </a>
    </li>
    {% else %}
    <li class="disabled">
      <a>
      &laquo; {% trans %}Previous{% endtrans %}
      </a>
    </li>
    {% endif %}

    {%- for page in pagination.iter_pages(left_edge=0, left_current=5, right_current=5, right_edge=0) %}
      {% if loop.first and page !=1 %}
      <li class="disabled"><a>...</a></li>
      {% endif %}

      {% if page %}
      <li {% if page == pagination.page %}class="active"{% endif %}>
        <a href="{{ url_for(endpoint, uri=uri, page=page, **kwargs) }}">{{ page }}</a>
      </li>
      {% endif %}

      {% if loop.last and page != pagination.pages %}
      <li class="disabled"><a>...</a></li>
      {% endif %}
    {% endfor %}

    {% if pagination.has_next -%}
    <li>
      <a class="" href="{{ url_for(endpoint, uri=uri, page=pagination.next_num, **kwargs) }}">
        {% trans %}Next{% endtrans %} &raquo;
      </a>
    </li>
    {% else %}
    <li class="disabled">
      <a>{% trans %}Next{% endtrans %} &raquo;</a>
    </li>
    {% endif %}

  </ul>
</div>
{% endmacro %}

{% macro  render_address(address) %}     
<address>
  <strong>{{ address.name }}</strong><br>
  {{ address.street }}<br>
  {% if address.streetbis %}
  {{ address.streetbis }}<br>
  {% endif %}
  {{ address.city }} {{ address.zip }}
  {% if address.subdivision %}
  {% endif %}
  {% if address.country %}
  <abbr title="{{ address.country.name }}">{{ address.country.code }}</abbr> 
  {% endif %}
</address> 
{% endmacro %}
