<p>This could be achieved through a recursive macro:</p>

<pre><code>{% macro recursive_nav(entries, depth) %}
{% import _self as self %}
&lt;ul&gt;
    {% for e in entries %}
        &lt;li&gt;
            &lt;a href="{{ e.url }}"&gt;{{ e.title }}&lt;/a&gt;
            {% if e.hasDescendants() %}
                {{ self.recursive_nav(e.children, depth+1) }}
            {% endif %}
        &lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
{% endmacro %}
</code></pre>

<p>And then to use it in your template:</p>

<pre><code>{% import '_macros/recursive-nav.twig' as rn %}
{% set entry = craft.entries.section('people').level(1) %}
&lt;div class="panel"&gt;
    &lt;h2&gt;People&lt;/h2&gt;
    {{ rn.recursive_nav(entry, 1) }}
&lt;/div&gt;
</code></pre>

<p>This was borrowed and slightly modified from Marion's answer <a href="https://craftcms.stackexchange.com/questions/1733/custom-or-complex-nav-tag/1734#1734">here</a>. That question is also a great resource for other ways to achieve this result.</p>

CC BY-SA 3.0
2014-09-09T14:10:16.603Z
Great answer. I guess this isn't possible without the macro unless you know how many levels deep you'll be going.
hamishtaplin
CC BY-SA 3.0
2014-09-09T14:11:54.967Z
Right because the macro is used for recursion. If you know how many levels you're going you can just keep looping. You could also setup a macro to only loop a finite amount or accept a number of levels as a variable.
Peter Tell
CC BY-SA 3.0
2014-09-09T14:02:21.767Z
2014-09-09T14:02:21.767Z
2017-04-13T12:50:59.930Z
Peter Tell
<p>Craft's built-in <code>{% nav %}</code> tag would save you having to write your own recursive macro:</p>

<p>Adapted from <a href="http://buildwithcraft.com/docs/templating/nav" rel="nofollow">http://buildwithcraft.com/docs/templating/nav</a></p>

<pre><code>&lt;div class="panel"&gt;
    &lt;h2&gt;People&lt;/h2&gt;
    &lt;ul&gt;
        {% nav entry in craft.entries.section('people') %}
            &lt;li&gt;
                &lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;
                {% ifchildren %}
                    &lt;ul&gt;
                        {% children %}
                    &lt;/ul&gt;
                {% endifchildren %}
            &lt;/li&gt;
        {% endnav %}
    &lt;/ul&gt;
&lt;/div&gt;
</code></pre>

<p>It's not quite as flexible, but is more readable. It should be all you need if it's just links to the entries you want, and it <em>might</em> be a tiny bit faster, but I haven't actually compared twig's compiled macro vs it's tag code.</p>

CC BY-SA 3.0
2014-09-10T10:00:43.543Z
Amazing, will try this.
hamishtaplin
CC BY-SA 3.0
2014-09-09T15:18:33.330Z
2014-09-09T15:32:26.750Z
2014-09-09T15:32:26.750Z
Mike Pepper
<p>Given a structure that has, for example, three levels of content—how do I just iterate over it and create a nested <code>&lt;ul&gt;</code> reflecting the hierarchy and containing the <code>title</code> and <code>slug</code> (as a link) of each entry?</p>

<p>The following just gives me a flat representation of the structure:</p>

<pre><code>&lt;div class="panel"&gt;
    &lt;h2&gt;People&lt;/h2&gt;
    &lt;ul&gt;
        {% for entry in craft.entries.section('people') %}
            &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
&lt;/div&gt;
</code></pre>

<structure>
Output structure in a <ul>
# Output structure in a <ul>

(Asked by: username_0 on 2014-09-09)

Given a structure that has, for example, three levels of content—how do I just iterate over it and create a nested  reflecting the hierarchy and containing the title and slug (as a link) of each entry?
The following just gives me a flat representation of the structure:

    People
    
        {% for entry in craft.entries.section('people') %}
            {{ entry.title }}
        {% endfor %}

**Tags:** structure


---

## 2 answers

### Answer by username_2 on 2014-09-09. Score: 6 | Accepted answer

Craft's built-in {% nav %} tag would save you having to write your own recursive macro:
Adapted from http://buildwithcraft.com/docs/templating/nav

    People
    
        {% nav entry in craft.entries.section('people') %}
            
                {{ entry.title }}
                {% ifchildren %}
                    
                        {% children %}
                    
                {% endifchildren %}
            
        {% endnav %}
    


It's not quite as flexible, but is more readable. It should be all you need if it's just links to the entries you want, and it might be a tiny bit faster, but I haven't actually compared twig's compiled macro vs it's tag code.

> **Comments:**
> **username_0**: Amazing, will try this.  

---

### Answer by username_1 on 2014-09-09. Score: 6

This could be achieved through a recursive macro:
{% macro recursive_nav(entries, depth) %}
{% import _self as self %}

    {% for e in entries %}
        
            {{ e.title }}
            {% if e.hasDescendants() %}
                {{ self.recursive_nav(e.children, depth+1) }}
            {% endif %}
        
    {% endfor %}

{% endmacro %}

And then to use it in your template:
{% import '_macros/recursive-nav.twig' as rn %}
{% set entry = craft.entries.section('people').level(1) %}

    People
    {{ rn.recursive_nav(entry, 1) }}


This was borrowed and slightly modified from Marion's answer here. That question is also a great resource for other ways to achieve this result.

> **Comments:**
> **username_0**: Great answer. I guess this isn't possible without the macro unless you know how many levels deep you'll be going.  
> **username_1**: Right because the macro is used for recursion. If you know how many levels you're going you can just keep looping. You could also setup a macro to only loop a finite amount or accept a number of levels as a variable.  

---

