{% if tree_top is undefined %}
  {% set tree_top = pages|list|first %}
{% endif %}

{% macro page_w_children(pg, n) %}
  {% if pg.is_ancestor_of(currentpage.html) %}
    {% set active_parent=True %}
  {% else %}
    {% set active_parent=False %}
  {% endif %}

  <div class="nav-item {% if currentpage == pg %}active{% elif active_parent%}active-parent{% endif %}">
    {% if not pg.children %}
    <a class="nav-link nav-leaf" href="{% if "//" not in pg.html %}{{currentpage.prefix}}{% endif %}{{pg.html}}">{{ pg.name }}</a>
    {% else %}


      <a class="nav-toggler {% if not active_parent and currentpage != pg %}collapsed{% endif %}" role="button" data-toggle="collapse" href="#tree_nav_group_{{n}}" aria-expanded="{% if active_parent or currentpage == pg %}true{% else %}false{% endif %}" aria-controls="tree_nav_group_{{n}}"></a>
    <a class="nav-link" href="{% if "//" not in pg.html %}{{currentpage.prefix}}{% endif %}{{pg.html}}">{{ pg.name }}</a>

    <nav class="nav flex-column {% if active_parent or pg == currentpage %}show {% else %}collapse{% endif %}" id="tree_nav_group_{{n}}">
    {% for child in pg.children %}
      {{ page_w_children(child, n~"_"~loop.index) }}
    {% endfor %}
    </nav>

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

<nav class="nav flex-column dactyl-tree-nav">
  <div class="nav-item nav-parent">
    <a class="nav-link" href="{% if "//" not in tree_top.html %}{{currentpage.prefix}}{% endif %}{{tree_top.html}}">{{tree_top.name}}</a>
  </div>
  {% for child in tree_top.children %}
    {{ page_w_children(child, loop.index) }}
  {% endfor %}
</nav>
