Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Using Multiple MiniJijna Templates Via Includes

This is a basic example of how to use multiple templates in Minijinja via includes. (It looks like the main way things are done is with extends blocks. I haven't been able to get them to work yet.)

Code

use minijinja::Environment;
use serde_json::{Result, Value};

pub fn multiple_templates_with_includes() -> Result<()> {
    let mut env = Environment::new();

    let base_string = r#"
<div>Hello, {% include "name" %}</div>
"#;

    let name_block_string = r#"
<strong>World</strong>
"#;

    env.add_template("base", base_string).unwrap();
    env.add_template("name", name_block_string).unwrap();

    let json_string = r#"{ "name": "World" }"#;
    let json_data: Value = serde_json::from_str(json_string)?;

    let tmpl = env.get_template("base").unwrap();
    println!("{}", tmpl.render(json_data).unwrap());
    Ok(())
}

One possible way to use multiple templates in the MiniJinja Rust crate