Skip to content

MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2

License

Notifications You must be signed in to change notification settings

mitsuhiko/minijinja

Folders and files

NameName
Last commit message
Last commit date

Latest commit

481d32a · Jul 21, 2024
Jan 6, 2023
Apr 28, 2024
Jul 18, 2024
Dec 27, 2021
Apr 8, 2024
Jul 21, 2024
Apr 8, 2024
Jul 2, 2024
Jul 2, 2024
Jul 2, 2024
Jul 2, 2024
Jul 2, 2024
Jul 2, 2024
Jul 21, 2024
Apr 2, 2024
Oct 29, 2023
Mar 29, 2024
Jul 21, 2024
Jun 9, 2024
Apr 4, 2024
Jul 19, 2024
Jul 19, 2024
Jun 13, 2024
Sep 23, 2021
Jul 21, 2024
Jul 21, 2024
Apr 8, 2024
Sep 23, 2022

Repository files navigation

MiniJinja: a powerful template engine for Rust with minimal dependencies

Build Status License Crates.io rustc 1.61.0 Documentation

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 can be used entirely without dependencies though by default it uses serde's Serialize trait for value conversions. 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 (/Users/mitsuhiko/Development/minijinja/examples/minimal)
└── minijinja v2.0.3 (/Users/mitsuhiko/Development/minijinja/minijinja)

Additionally minijinja is also available as an (optionally pre-compiled) command line executable called minijinja-cli:

$ curl -sSfL https://github.com/mitsuhiko/minijinja/releases/latest/download/minijinja-cli-installer.sh | sh
$ echo "Hello {{ name }}" | minijinja-cli - -Dname=World
Hello World

You can play with MiniJinja online in the browser playground powered by a WASM build of MiniJinja.

Goals:

Example

Example Template:

{% extends "layout.html" %}
{% block body %}
  <p>Hello {{ name }}!</p>
{% endblock %}

Invoking from Rust:

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

Use Cases and Users

Here are some interesting Open Source users and use cases of MiniJinja. The examples link directly to where the engine is used so you can see how it's utilized:

Getting Help

If you are stuck with MiniJinja, have suggestions or need help, you can use the GitHub Discussions.

Related Crates

Similar Projects

These are related template engines for Rust:

  • Askama: Jinja inspired, type-safe, requires template precompilation. Has significant divergence from Jinja syntax in parts.
  • Tera: Jinja inspired, dynamic, has divergences from Jinja.
  • TinyTemplate: minimal footprint template engine with 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.

Upgrading from MiniJinja 1.x

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.

Sponsor

If you like the project and find it useful you can become a sponsor.

License and Links