jinja2 recursive loop vs dictionary

You're right, dictionary isn't being updated in the recursion calls, and the loop cannot continue because the keys aren't found.

A workaround to this problem is using just the variables assigned in the for loop. In the dictionary example, this means to iterate through the items of the dictionary instead of just the keys:

from jinja2 import Template

template = Template("""                                                     
{%- for key, value in dictionary.items() recursive %}                       
  <li>{{ key }}                                                             
    {%- if value %}                                                         
      Recursive {{ key }}, {{value}}                                        
      <ul>{{ loop(value.items())}}</ul>                                     
    {%- endif %}                                                            
  </li>                                                                     
{%- endfor %}                                                               
""")

print template.render(dictionary={'a': {'b': {'c': {}}}})

The output of this script is:

<li>a
    Recursive a, {'b': {'c': {}}}
    <ul>
<li>b
    Recursive b, {'c': {}}
    <ul>
<li>c
</li></ul>
</li></ul>
</li>

where you can see that recursion on the b key works fine because both key and value are updated on each iteration of the loop (I added the "Recursive key, value" message to the template to make it clear).

Tags:

Python

Jinja2