- Notifications
You must be signed in to change notification settings - Fork111
MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2
License
mitsuhiko/minijinja
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
MiniJinja is a powerful but minimal dependency template engine for Rust whichis based on the syntax and behavior of theJinja2 template engine for Python.
It's implemented on top ofserde
and only has it as a single requireddependency. It supportsa range of features from Jinja2including inheritance, filters and more. The goal is that it should be possibleto use some templates in Rust programs without the fear of pulling in complexdependencies for a small problem. Additionally it tries not to re-inventsomething but stay in line with prior art to leverage an already existingecosystem of editor integrations.
$ cargo treeminimal v0.1.0 (examples/minimal)└── minijinja v2.8.0 (minijinja) └── serde v1.0.144
Additionally minijinja is also available as an (optionally pre-compiled) command line executablecalledminijinja-cli
:
$ curl -sSfL https://github.com/mitsuhiko/minijinja/releases/latest/download/minijinja-cli-installer.sh | sh$ echo "Hello {{ name }}" | minijinja-cli - -Dname=WorldHello World
You can play with MiniJinja onlinein the browser playgroundpowered by a WASM build of MiniJinja.
Goals:
- Well documented, compact API
- Minimal dependencies, reasonable compile times anddecent runtime performance
- Stay as close as possible to Jinja2
- Support forexpression evaluation whichallows the useas a DSL
- Support for all
serde
compatible types - Well tested
- Support fordynamic runtime objects with methods and dynamic attributes
- Descriptive errors
- Bindings forJavaScript,Python, andC
- Comes with a handyCLI
- Compiles to WebAssembly
Example Template:
{%extends"layout.html"%}{%blockbody%} <p>Hello {{ name }}!</p>{%endblock%}
Invoking from Rust:
use minijinja::{Environment, context};fnmain(){letmut 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());}
Here are some interesting Open Source users and use cases of MiniJinja. The examples link directly to wherethe engine is used so you can see how it's utilized:
HTML Generation:
- Zine uses it togenerate static HTML
- Oranda uses it togenerate HTML landing pages
Code Generation:
- OpenTelemetry's Weaver uses it togenerate documentation, code and other outputs from the OTel specification.
Structure Generation:
- Astral's Rye is using it togenerate project structures
- Maturin uses it togenerate project structures
- cargo-dist uses it togenerate CI and project configuration
AI Chat Templating:
- HuggingFace uses it torender LLM chat templates
- mistral.rs uses it torender LLM chat templates
- BoundaryML's BAML uses it torender LLM chat templates
- LSP-AI uses it torender LLM chat templates
- LoRAX uses it torender LLM chat templates
- tensorzero uses it torender LLM and system templates
Data and Processing:
If you are stuck withMiniJinja
, have suggestions or need help, you can use theGitHub Discussions.
- minijinja-autoreload: providesauto reloading functionality of environments
- minijinja-embed: providesutilities for embedding templates in a binary
- minijinja-contrib: providesadditional utilities too specific for the core
- minijinja-py: makes MiniJinjaavailable to Python
- minijinja-js: makes MiniJinjaavailable to JavaScript via WASM (for Node and Browser)
- minijinja-cli: a command line utility.
- minijinja-cabi: a C binding to MiniJinja.
These are related template engines for Rust:
- Askama: Jinja inspired, type-safe, requires templateprecompilation. Has significant divergence from Jinja syntax in parts.
- Rinja: Jinja inspired, type-safe, requires templateprecompilation. Has significant divergence from Jinja syntax in parts.
- Tera: Jinja inspired, dynamic, has divergences from Jinja.
- TinyTemplate: minimal footprint template enginewith syntax that takes lose inspiration from Jinja and handlebars.
- Liquid: an implementation of Liquid templates for Rust.Liquid was inspired by Django from which Jinja took it's inspiration.
If you like the project and find it useful you canbecome asponsor.
About
MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2