Recursive Macro In Askama Template?

⚓ Rust    📅 2025-12-16    👤 surdeus    👁️ 2      

surdeus

I'm trying to make a recursive macro in an askama template, so that I can render a filetree via a recursive walk.

I have a FileTree data type like this:

pub struct FileTree {
    pub name: String,
    pub path: PathBuf,
    pub is_dir: bool,
    pub children: Vec<FileTree>,
}

In Askama, I have a template like this:

{% macro render_node(node) %}
    {% if node.is_dir %}
       {{ node.name }}
       {% for child in node.children %}
           {{ call render_node(child) }}
       {% endfor %}
    {% else %}
       {% include "file.html" %}
    {% endif %}`
{% endmacro %}

{% call render_node(filetree) %}

But at compile time, I get the error:

error: unknown node `render_node`
         --> templates/macros.html:34:28
       "render_node(child) }}\n                {% endfor %}\n            </ol>\n        </d"...

Which seems to tell me that the macro cannot call itself from within itself. Is there a way to do this in askama? Or a different syntax I need?

1 post - 1 participant

Read full topic

🏷️ Rust_feed