Based on my current reading of askama, I am not sure if this type of recursion is possible (see: Template syntax - Askama), perhaps due to the way it compiles templates, but I am not sure.
I also tried doing a recursive include instead of a recursive macro, which lead to compilation crashing.
Instead, I have found a working solution I am happy with that does the recursion in Rust, outside of askama, but calling .render() on the templates at the right time.
Something like:
filetree.rs
use askama::Template;
#[derive(Template)]
#[template(path = "file.html")]
pub struct FileTemplate {
pub name: String,
}
#[derive(Template)]
#[template(path = "directory.html")]
pub struct DirectoryTemplate {
pub name: String,
pub children_html: String,
}
pub fn render_node(node: &FileTree) -> askama::Result<String> {
// case for files
if !node.is_dir {
let template = FileTemplate {
name: node.name.to_string(),
};
template.render()
}
// case for directories
else {
let mut children_html = String::new();
for child in &node.children {
children_html.push_str(&render_node(child)?);
}
let template = DirectoryTemplate {
name: node.name.to_string(),
children_html: children_html,
};
template.render()
}
}