{#
  Themes hierarchical taxonomy terms as nested <ul> elements.

  This template is intended to be called from within another template to provide
  the "scaffolding" of nested lists while the calling template provides the
  actual filter element via the 'item' variable.

  Available variables:
    - current_nesting_level: the nesting level of the most recently printe item.
    - new_nesting_level: the nesting level of the item to print.
    - item: The item to print.
    - loop: The loop variable from the iterator that calls this template.
      Requires the loop.first and loop.last elements.
#}
{% spaceless %}
  {% set delta = (current_nesting_level - new_nesting_level) | abs %}
  {% if loop.first %}
    <ul>
  {% else %}
    {% if delta %}
      {% for i in 1..delta %}
        {% if new_nesting_level > current_nesting_level  %}
          <ul>
        {% else %}
          </li></ul>
        {% endif %}
      {% endfor %}
    {% endif %}
  {% endif %}

  <li>{{ item }}

  {% if loop.last %}
    {# Close any remaining <li> tags #}
    {% for i in new_nesting_level..0 %}
      </li>
    {% endfor %}
    </ul>
  {% endif %}
{% endspaceless %}
