title: Recursively find child items and list in jinja
body: <p>I'm trying to display "notes" in a nested list. Each note has a property called <code>parentID</code> that indicates a note that it is nested under.</p>
  
  <p>Currently I am achieving a single level nest by doing this:</p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>class Note(Model):
      title = CharField()
      tags = CharField()
      content = TextField()
      parentID = IntegerField(default=0)
  
      class Meta:
          database = db
  </code></pre>
  
  <p><strong>app.py</strong></p>
  
  <pre><code>def getSubnotes(note):
      note_id = note.id
      subnotes = models.Note.select().where(models.Note.parentID == note_id)
      return subnotes
  
  app.jinja_env.globals.update(getSubnotes=getSubnotes)
  </code></pre>
  
  <p><strong>Index.html</strong></p>
  
  <pre><code>&lt;div class="row"&gt;
  &lt;div class="medium-12 medium-centered"&gt;
    &lt;ul&gt;
      {% for note in notes %}
        {% if note.parentID == 0 %}
        &lt;li&gt;&lt;a href="/n/{{note.getHash()}}"&gt;{{ note.title }}&lt;/a&gt;&lt;/li&gt;
        &lt;ul&gt;
          {% for subnote in getSubnotes(note) %}
          &lt;li&gt;&lt;a href="/n/{{subnote.getHash()}}"&gt;{{ subnote.title }}&lt;/a&gt;&lt;/li&gt;
          {% endfor %}
        &lt;/ul&gt;
        {% endif %}
      {% endfor %}
    &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>I would like to, however, recursively find all children notes of a single note and list them out, so there are nests of nests.</p>
  
  <p>I have seen this as an example of how to recursively list in jinja (from <a href="http://jinja.pocoo.org/docs/2.9/templates/#for" rel="nofollow noreferrer">jinja docs</a>):</p>
  
  <pre><code>&lt;ul class="sitemap"&gt;
  {%- for item in sitemap recursive %}
      &lt;li&gt;&lt;a href="{{ item.href|e }}"&gt;{{ item.title }}&lt;/a&gt;
      {%- if item.children -%}
          &lt;ul class="submenu"&gt;{{ loop(item.children) }}&lt;/ul&gt;
      {%- endif %}&lt;/li&gt;
  {%- endfor %}
  &lt;/ul&gt;
  </code></pre>
  
  <p>However I am confused about what <code>.children</code> actually is. How is it referencing itself or <code>items</code> of the same type?</p>
  
  <p>How would I go about doing this recursively or is there a better method for achieving the same thing?</p>
  
  <p>Any help is very much appreciated!</p>