question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recursive for loops

See original GitHub issue

Since there is no while nor in Jinja, nor in Nunjucks, and without it (or functions) it’s quite tricky (or maybe even impossible) to, let’s say, output multilevel object (which can be, for example, sitemap), it would be very nice to have recursive flag for loops, as per http://jinja.pocoo.org/docs/dev/templates/#for

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

27reactions
jbmoelkercommented, Apr 6, 2016

Just like a function a macro can call itself recursively. So I’d argue there is no need for a custom “recursive” tag in the Nunjucks core library. Here’s an example of solving this with normal macro:

<nav>
    <h2>Site menu</h2>
    <ul>
        {% for item in menu %}
        {{ menuItem(item) }}
        {% endfor %}
    </ul>
</nav>

{% macro menuItem(item) %}
<li>
    {{ item.text }}
    {% if item.items %}
    <ul>
        {% for item in item.items %}
        {{ menuItem(item) }}
        {% endfor %}
    </ul>
    {% endif %}
</li>
{% endmacro %}

See Live example of menu with nested items using Nunjucks macro recursively.

0reactions
fdintinocommented, May 16, 2018

I have opted not to close this because it is a feature in jinja2 and adding it would increase syntax parity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How Recursion Works Inside a For Loop - Stack Overflow
The loop controls horizontally how many branches at generated while the recursion decides the height of the tree. The tree is generated along ......
Read more >
Recursion and Looping | Baeldung on Computer Science
In simple terms, we can define looping or iteration as the process where the same set of instructions is repeated multiple times in...
Read more >
General way to convert a loop (while/for) to recursion or from a ...
Actually you should break the function down first: A loop has a few parts: the header, and processing before the loop.
Read more >
Replace Iteration with Recursion - Refactoring
The solution is to replace the iteration with recursion. Unlike most procedural looping constructs, a recursive function call can be given a meaningful...
Read more >
How does recursion work inside a for loop? Most importantly ...
A loop is working in a recursion as long as the loop variables are all local and therefore on the stack. Every call...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found