{% macro form_fields(form, only=none, exclude=none, form_group_class='form-group', show_label=true, placeholder=false) %}
  {% if exclude is not iterable %}
    {% set exclude = [] %}
  {% endif %}
  {% for field in form %}
    {% if field.widget.input_type == 'hidden' %}
      {{ field }}
    {% else %}
      {% if field.name not in exclude and (not only or field.name in only) %}
        {% if placeholder %}
          {% set placeholder = field.label.text %}
        {% endif %}
        {{ form_field(field, form_group_class=form_group_class, show_label=show_label, placeholder=placeholder) }}
      {% endif %}
    {% endif %}
  {% endfor %}
{% endmacro %}


{% macro form_errors(field) %}
  {%- if field.errors %}
    <div class="invalid-feedback">
      {%- for error in field.errors %}
        <p>{{ error }}</p>
      {% endfor -%}
    </div>
  {% endif -%}
{% endmacro %}


{% macro form_label(label, label_class='', required=false) %}
  {% if required %}
    {% set label_class = label_class + ' required' %}
  {% endif %}
  {{ label(class=label_class) }}
{% endmacro %}


{% macro form_check(field, valid_default_class='') %}
  {% set valid_class = 'is-invalid' if field.errors else valid_default_class %}
  {% set control_class = 'custom-checkbox' if field.widget.input_type == 'checkbox' else 'custom-radio' %}
  <div class="custom-control {{ control_class }}">
    {{ field(class='custom-control-input ' + valid_class) }}
    {{ field.label(class='custom-control-label') }}
    {{ form_errors(field) }}
  </div>
{% endmacro %}


{% macro form_only_field(field, placeholder=false, addon_before=none, addon_after=none) %}
  {% set valid_class = 'is-invalid' if field.errors else '' %}

  {% if field.render_kw and field.render_kw.get('data-icon') %}
    {% set addon_after %}<span class="{{ field.render_kw.get('data-icon') }}"></span>{% endset %}
  {% endif %}

  {% if addon_before %}
    <div class="input-group-prepend">
      <div class="input-group-text">{{ addon_before|safe }}</div>
    </div>
  {% endif %}

  {% if placeholder is true %}
    {% set placeholder = field.label.text %}
  {% endif %}

  {% if field.widget.input_type == 'file' %}
    <div class="custom-file">
      {{ field(class="custom-file-input " + valid_class) }}
      <label class="custom-file-label" for="{{ field.id }}">{{ field.data or 'Choose file' }}</label>
    </div>
  {% else %}
    {{ field(class="form-control " + valid_class, placeholder=placeholder) }}
  {% endif %}

  {% if field.description %}
    {% if field.description is iterable and field.description is not string %}
      <ul class="form-text text-muted text-sm">
        {% for i in field.description %}
          <li>{{ i }}</li>
        {% endfor %}
      </ul>
    {% else %}
      <small class="form-text text-muted">{{ field.description }}</small>
    {% endif %}
  {% endif %}

  {% if addon_after %}
    <div class="input-group-append">
      <div class="input-group-text">{{ addon_after|safe }}</div>
    </div>
  {% endif %}

  {{ form_errors(field) }}
{% endmacro %}


{% macro form_field(field, form_group_class='form-group', placeholder=false, show_label=true,
               addon_before=none, addon_after=none) %}
  <div class="{{ form_group_class }}">
    {% if show_label %}
      {{ form_label(field.label, required='required' in field.flags) }}
    {% endif %}
    {{ form_only_field(field, placeholder=placeholder, addon_before=addon_before, addon_after=addon_after) }}
  </div>
{% endmacro %}


{% macro breadcrumb(title='', url='', icon='', is_active=false, position=none) %}
  {% set label|trim %}
    {% if icon %}<i class="{{ icon }}"></i>{% endif %}
    {% if title %}<span itemprop="name">{{ title|safe }}</span>{% endif %}
  {% endset %}

  {% if label and not url %}
    {% set body = label %}
  {% elif not label and not url %}
    {% set body = caller() %}
  {% else %}
    {% set body %}<a itemprop="item" href="{{ url }}">{{ label }}</a>{% endset %}
  {% endif %}

  <li class="breadcrumb-item{{ is_active|if_true(' active') }}"
      {% if is_active %} aria-current="page"{% endif %}
      itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"
  >
    {{- body -}}
    {%- if position -%}
      <meta itemprop="position" content="{{ position }}">
    {%- endif -%}
  </li>
{% endmacro %}


{% macro pagination(pages, pagination_class='') %}
  <ul class="pagination{% if pagination_class %} {{ pagination_class }}{% endif %}">
    {% for item in pages %}
      <li class="page-item{% if item.is_active %} active{% endif %}{% if item.disabled %} disabled{% endif %}">
        <a class="page-link" href="{{ item.url }}">
          <span class="d-md-none">{{ item.responsive|safe }}</span>
          <span class="d-none d-md-block">{{ item.text|safe }}</span>
          {% if item.is_active %} <span class="sr-only">(current)</span>{% endif %}
        </a>
      </li>
    {% endfor %}
  </ul>
{% endmacro %}