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

{% macro page_w_children(pg, n) %}
  {% if not (pg.nav_omit and pg != currentpage) %}
  {% 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 %}
      {% if "//" in pg.html %}
    <a class="nav-link nav-leaf external-link" target="_blank" href="{% if "//" not in pg.html %}{{currentpage.prefix}}{% endif %}{{pg.html}}">{{ pg.name }}{% if pg.status is defined %} {% include 'component-status_'+pg.status+'.html.jinja' %}{% endif %} <i aria-hidden="true" class="fa fa-external-link"></i></a>
      {% else %}
    <a class="nav-link nav-leaf" href="{% if "//" not in pg.html %}{{currentpage.prefix}}{% endif %}{{pg.html}}">{{ pg.name }}{% if pg.status is defined %} {% include 'component-status_'+pg.status+'.html.jinja' %}{% endif %}</a>
      {% endif %}
    {% 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>

      {% if "//" in pg.html %}
        <a class="nav-link external-link" target="_blank" href="{% if "//" not in pg.html %}{{currentpage.prefix}}{% endif %}{{pg.html}}">{{ pg.name }}{% if pg.status is defined %} {% include 'component-status_'+pg.status+'.html.jinja' %}{% endif %} <i aria-hidden="true" class="fa fa-external-link"></i></a>
      {% else %}
        <a class="nav-link" href="{% if "//" not in pg.html %}{{currentpage.prefix}}{% endif %}{{pg.html}}">{{ pg.name }}{% if pg.status is defined %} {% include 'component-status_'+pg.status+'.html.jinja' %}{% endif %}</a>
      {% endif %}

    <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>
  {% endif %}
{% 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}}{% if tree_top.status is defined %} {% include 'component-status_'+tree_top.status+'.html.jinja' %}{% endif %}</a>
  </div>
  {% for child in tree_top.children %}
    {{ page_w_children(child, loop.index) }}
  {% endfor %}
</nav>
