Skip to content Skip to sidebar Skip to footer

How To Do Nested Comments In Flask/jinja?

Like the comments in Hacker News and Reddit. I've looked at Jinja's docs but I can't find anything about recursion (which I assume is how this sort of thing is done). Any ideas? Th

Solution 1:

Unless, you give an example how your comment data is laid out, I can only give a basic example how recursive for loops work:

{%- for item in comments recursive %}
    <li>{{ item.text }}</li>
    {%- if item.children -%}
        <ulclass="children">{{ loop(item.children) }}</ul>
    {%- endif %}</li>
{%- endfor %}

Solution 2:

Use macros, they support recursion. http://jinja.pocoo.org/docs/templates/#macros

Edit: for loops also support recursion, this would work as well. http://jinja.pocoo.org/docs/templates/#for

Post a Comment for "How To Do Nested Comments In Flask/jinja?"