{% set items = namespace(sections=[], pages=[]) %} {# sort children into pages and sections #} {% for item in nav %} {% if item.is_section %} {% set items.sections = items.sections + [item] %} {% elif item != page %} {% set items.pages = items.pages + [item] %} {% endif %} {% endfor %} {# add heading #}

{{config.extra.toc_heading or "Contents"}}

{# add standalone pages first #} {% if nav_pages|length %}
{% for item in items.pages %}
  • {{ item.title }}
  • {% endfor %}
    {% endif %} {# add sections #} {% for item in items.sections %}

    {{ item.title }}

    {% for subitem in item.children %} {# use a namespace so that we can set stuff within a sub-for loop #} {% set index = namespace(obj=subitem) %} {# if subitem is a section, navigate to its index page #} {% if subitem.is_section %} {% set index.icon = "section" %} {% for child in subitem.children if child.is_index %} {% set index.obj = child %} {% endfor %} {% endif %} {# create list item #} {% if subitem != item.children[0] %}
  • {{ index.obj.title }}
  • {% endif %} {% endfor %}
    {% endfor %}