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.

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.

Code

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

References