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.

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

TODO: Pull subtitle into page object
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()
}

Notes

Debugging Stuff

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

        -- title

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


-- notes

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

- The approach uses `render_str()`rust` which is a one shot
for the render. If the template will be used multiple times
it's more efficient to use `add_template()`rust` 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^^

-- ref
-- id: op
-- pid: 2noaza1kjtyk




-- blurb

-- categories
-- Rust 
-- Minijinja 

-- metadata
-- date: 2023-05-08 14:33:07
-- id: 2pwrkwua
-- site: aws
-- type: post
-- status: published