I am using the Jinja 2.8 templating engine. I am trying to write a template that will move through a tree structure and output information from this tree. For this, I'm trying to use a macro that calls itself, which doesn't seem to work.
This simple recursive macro also does not work:
{% macro factorial(n) %}
{% if n > 1 %}
{{ n }} * {{ factorial(n-1) }}
{% endif %}
{% endmacro %}
{{ factorial(3) }}
When you run the next error, it rises on the third line of Jinja code.
UndefinedError: 'factorial' is undefined
Does Jinja support recursive macros? How else can you lay out a nested data structure in Jinja?
source
share