Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

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

rust
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