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.

Use Multiple MiniJinja Templates Via Extends

Took me a while to figure this one out. The key is to call the template with the [TODO: Code shorthand span ] tag in it instead of the base one that is being extended.

It's easier for me to get my head around the [TODO: Code shorthand span ] method. An example of how to do that is here : (TODO : link post : 2nohlvaqizro)

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

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

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

    let name_block_string = r#"{% extends "base" %}
{% block name %}<strong>World</strong>{% endblock %}
"#;

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

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

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