The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Use Multiple MiniJinja Templates Via Extends

TODO: Pull subtitle into page object

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

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Use Multiple MiniJinja Templates Via Extends


-- p

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



-- blurb

-- categories
-- Rust
-- Minijinja

-- metadata
-- date: 2023-03-22 19:53:37
-- id: 2nojvmsm
-- site: aws
-- type: post
-- status: draft