Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-09T14:10:16.603Z
Answers.Text: Great answer. I guess this isn't possible without the macro unless you know how many levels deep you'll be going.
Answers.UserDisplayName: hamishtaplin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-09T14:11:54.967Z
Answers.Text: 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.
Answers.UserDisplayName: Peter Tell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-09T14:02:21.767Z
Answers.LastActivityDate: 2014-09-09T14:02:21.767Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: Peter Tell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-10T10:00:43.543Z
Answers.Text: Amazing, will try this.
Answers.UserDisplayName: hamishtaplin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-09T15:18:33.330Z
Answers.LastActivityDate: 2014-09-09T15:32:26.750Z
Answers.LastEditDate: 2014-09-09T15:32:26.750Z
Answers.UserDisplayName: Mike Pepper
Body: <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>
  
Tags: <structure>
Title: Output structure in a <ul>
ThreadText: # 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 `<ul>` 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:
  
  ```
  <div class="panel">
      <h2>People</h2>
      <ul>
          {% for entry in craft.entries.section('people') %}
              <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
          {% endfor %}
      </ul>
  </div>
  
  ```
  
  **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>
  
  ```
  <div class="panel">
      <h2>People</h2>
      <ul>
          {% nav entry in craft.entries.section('people') %}
              <li>
                  <a href="{{ entry.url }}">{{ entry.title }}</a>
                  {% ifchildren %}
                      <ul>
                          {% children %}
                      </ul>
                  {% endifchildren %}
              </li>
          {% endnav %}
      </ul>
  </div>
  
  ```
  
  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 %}
  <ul>
      {% for e in entries %}
          <li>
              <a href="{{ e.url }}">{{ e.title }}</a>
              {% if e.hasDescendants() %}
                  {{ self.recursive_nav(e.children, depth+1) }}
              {% endif %}
          </li>
      {% endfor %}
  </ul>
  {% endmacro %}
  
  ```
  
  And then to use it in your template:
  
  ```
  {% import '_macros/recursive-nav.twig' as rn %}
  {% set entry = craft.entries.section('people').level(1) %}
  <div class="panel">
      <h2>People</h2>
      {{ rn.recursive_nav(entry, 1) }}
  </div>
  
  ```
  
  This was borrowed and slightly modified from Marion's answer [here](https://craftcms.stackexchange.com/questions/1733/custom-or-complex-nav-tag/1734#1734). 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.  
  
  ---
  
  