home ~ projects ~ socials

Load Multiple MiniJinja Templates From A Directory

---
[dependencies]
minijinja = { version = "2.9.0", features = ["loader"] }
---

use minijinja::{Environment, context, path_loader};

fn main() {
  let template_dir = "/Users/alan/workshop/examples/minijinja-templates";
  let mut env = Environment::new();
  env.set_loader(path_loader(template_dir));
  let template = env.get_template("alfa.jinja").unwrap();
  let output = template.render(context!(name => "world")).unwrap();
  println!("{}", output);

}
Output:
Hello, world

Notes

  • The path in the template_dir variable has a file in it called alfa.html
  • The content of alfa.html is Hello, {{ name }}
  • The {{ name }} string gets replace with value of name from the context!()
-- end of line --