You can write a recursive macro to display the site structure. In that macro, when you are looping through the entries loop.index
will be the the loop index, and e.level
will be the depth of entry e
in the structure. Here is a macro to print the navigation.
{% macro recursiveNav(entries) %}
{% import _self as self %}
<ul>
{% for e in entries %}
{# loop.index is our index, e.level is our level #}
<li>
{# using twig range here to output right number of 0s #}
{% if e.level > 1 %}{% for i in range(2, e.level) %}0{% endfor %}{% endif %}{{loop.index}}
{{e.link}}
{% if e.hasDescendants %}
{{self.recursiveNav(e.children)}}
{% endif %}
</li>
{% endfor %}
</ul>
{% endmacro %}
{% import _self as self %}
{% set entries = craft.entries.section('sitecontent').level(1) %}
{# call recursive nav, pass it level 1 entries #}
{{ self.recursiveNav(entries) }}
If all you want is the title for a particular entry (02 strategy e.g.), you can use almost the same recursive macro to do that:
{% macro recursiveTitle(entries, distinguishedId) %}
{% import _self as self %}
{% for e in entries %}
{% if e.id == distinguishedId %}
{# this is the one we want #}
{% if e.level > 1 %}{% for i in range(2, e.level) %}0{% endfor %}{% endif %}{{loop.index}}
{{e.title}}
{% endif %}
{% if e.hasDescendants %}
{{self.recursiveTitle(e.children, distinguishedId)}}
{% endif %}
{% endfor %}
{% endmacro %}
{% import _self as self %}
{% set entries = craft.entries.section('sitecontent').level(1) %}
{# call recursive title, pass it level 1 entries and id of our active entry #}
{{ self.recursiveTitle(entries, entry.id) }}
There is no way to pass data out of a macro, so the recursiveNav can't also save the index and level of the active entry for later.