home
NOTE: Under Construction - I'm in the middle of upgrading my site and lots of stuff is kinda broken. Please forgive the mess.

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.)

March 2023
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(())
}
═══ § ═══