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.

Use Multiple MiniJinja Templates Via Extends

Took me a while to figure this one out. The key is to call the template with the `extends`` tag in it instead of the base one that is being extended.

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

Code

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(())
}