MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2
MiniJinja is a powerful but minimal dependency template engine for Rust which is based on the syntax and behavior of the Jinja2 template engine for Python.
It's implemented on top of serde
and only has it as a single required
dependency. It supports a range of features from Jinja2
including inheritance, filters and more. The goal is that it should be possible
to use some templates in Rust programs without the fear of pulling in complex
dependencies for a small problem. Additionally it tries not to re-invent
something but stay in line with prior art to leverage an already existing
ecosystem of editor integrations.
$ cargo tree
minimal v0.1.0 (examples/minimal)
└── minijinja v2.0.1 (minijinja)
└── serde v1.0.144
You can play with MiniJinja online in the browser playground powered by a WASM build of MiniJinja.
Goals:
serde
compatible types{% extends "layout.html" %}
{% block body %}
<p>Hello {{ name }}!</p>
{% endblock %}
use minijinja::{Environment, context};
fn main() {
let mut env = Environment::new();
env.add_template("hello.txt", "Hello {{ name }}!").unwrap();
let template = env.get_template("hello.txt").unwrap();
println!("{}", template.render(context! { name => "World" }).unwrap());
}
If you are stuck with MiniJinja
, have suggestions or need help, you can use the
GitHub Discussions.
There are two major versions of MiniJinja both of which are currently maintained. Most users should upgrade to 2.x which has a much improved object system. However if you have been using dynamic objects in the past the upgrade might be quite involved. For upgrade informations refer to UPDATING which has a guide with examples of what the changes between the two engine versions are.
To see examples and code from MiniJinja 1.x, you can browse the minijinja-1.x branch.
These are related template engines for Rust:
If you like the project and find it useful you can become a sponsor.