home ~ projects ~ socials

Use Multiple Templates With MiniJinja in Rust

This example could use some tightening up, but it shows how to do the thing. (Though it won't work out of the box because it calls neopolitan which you probably don't have.)

Took me a bit to figure out that the way to use multiple templates in MiniJinja is to load them with `add_template()rust but only call `get_template()rust and use it to kick things off.

#![allow(warnings)]
use minijinja::{context, Environment};
use neopolitan::structure::structure;
use std::fs;

fn main() {
    let mut env = Environment::new();

    let post_base_template = fs::read_to_string("src/templates/post/base.html").unwrap();
    &env.add_template("post_base", post_base_template.as_str());

    let post_title_template = fs::read_to_string("src/templates/post/title.html").unwrap();
    &env.add_template("post_title", post_title_template.as_str());

    let source = fs::read_to_string("content/_sample_alfa.neo").unwrap();
    let structure = structure(source.as_str()).unwrap().1;
    let post_base = &env.get_template("post_base").unwrap();
    println!(
        "{}",
        post_base.render(context!(wrapper => &structure)).unwrap()
    );
}
-- end of line --