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.

Include A Minijinja Template And JSON Data Directly In A Rust Binary

This is what I'm using for the initiail setup of my Bunkers And Badasses Virtual Dice Roller. It embeds a Minijinja template and JSON data directcly into the Rust binary.

rust
use minijinja::Environment;
use serde_json::Value;

fn main() {
    let template = include_str!("template.j2");
    let json = include_str!("data.json");
    let output = render(template, json);
    dbg!(output);
}

fn render(template: &str, json: &str) -> String {
    let data: Value = serde_json::from_str(json).unwrap();
    let env = Environment::new();
    env.render_str(template, data).unwrap()
}

- The approach is useful to me for making single site pages where I generate everything at build time

- The approach uses ` render _ str() [TODO: Code shorthand span ] which is a one shot for the render. If the template will be used multiple times it's more efficient to use ` add _ template() [TODO: Code shorthand span ] to load it once then use it multiple times p:2noaza1kjtyk

- The JSON data could be loaded dynamically for a more flexible approach

- And, of course, the templates could be loaded dynamically as well if they needed to change. That would lead to the code in the other post op

Footnotes And References