Skip to main content

An experimental Python binding of the Rust MiniJinja template engine.

Project description

MiniJinja for Python: a powerful template engine for Rust and Python

Build Status License Crates.io rustc 1.61.0 Documentation

minijinja-py is an experimental binding of MiniJinja to Python. It has somewhat limited functionality compared to the Rust version. These bindings use maturin and pyo3.

You might want to use MiniJinja instead of Jinja2 when the full feature set of Jinja2 is not required and you want to have the same rendering experience of a data set between Rust and Python.

With these bindings MiniJinja can render some Python objects and values that are passed to templates, but there are clear limitations with regards to what can be done.

To install MiniJinja for Python you can fetch the package from PyPI:

$ pip install minijinja

Basic API

The basic API is hidden behind the Environment object. It behaves almost entirely like in minijinja with some Python specific changes. For instance instead of env.set_debug(True) you use env.debug = True. Additionally instead of using add_template or attaching a source you either pass a dictionary of templates directly to the environment or a loader function.

from minijinja import Environment

env = Environment(templates={
    "template_name": "Template source"
})

To render a template you can use the render_template method:

result = env.render_template('template_name', var1="value 1", var2="value 2")
print(result)

Purpose

MiniJinja attemps a certain level of compatibiliy with Jinja2, but it does not try to achieve this at all costs. As a result you will notice that quite a few templates will refuse to render with MiniJinja despite the fact that they probably look quite innocent. It is however possible to write templates that render to the same results for both Jinja2 and MiniJinja. This raises the question why you might want to use MiniJinja.

The main benefit would be to achieve the exact same results in both Rust and Python. Additionally MiniJinja has a stronger sandbox than Jinja2 and might perform ever so slightly better in some situations. However you should be aware that due to the marshalling that needs to happen in either direction there is a certain amount of loss of information.

Dynamic Template Loading

MiniJinja's Python bindings inherit the underlying behavior of how MiniJinja loads templates. Templates are loaded on first use and then cached. The templates are loaded via a "source" (called loader in MiniJinja's Python bindings). To trigger a reload you can call env.reload() or alternatively set env.reload_before_render to True.

def my_loader(name):
    segments = []
    for segment in name.split("/"):
        if "\\" in segment or segment in (".", ".."):
            return None
        segments.append(segment)
    try:
        with open(os.path.join(TEMPLATES, *segments)) as f:
            return f.read()
    except (IOError, OSError):
        pass

env = Environment(loader=my_loader)
env.reload_before_render = True
print(env.render_template("index.html"))

Alternatively templates can manually be loaded and unloaded with env.add_template and env.remove_template.

Auto Escaping

The default behavior is to use auto escaping file files ending in .html. You can customize this behavior by overriding the auto_escape_callback:

env = Environment(auto_escape_callback=lambda x: x.endswith((".html", ".foo")))

MiniJinja uses markupsafe if it's available on the Python side. It will honor __html__.

State Access

Functions passed to the environment such as filters or global functions can optionally have the template state passed by using the pass_state parameter. This is similar to pass_context in Jinja2. It can be used to look at the name of the template or to look up variables in the context.

from minijinja import pass_state

@pass_state
def my_filter(state, value):
    return state.lookup("a_variable") + value

env.add_filter("add_a_variable", my_filter)

Runtime Behavior

MiniJinja uses it's own runtime model which is not matching the Python runtime model. As a result there are clear gaps in beahvior between the two and only limited effort is made to bridge them. For instance you will be able to call some methods of types, but for instance builtins such as dicts and lists do not expose their methods on the MiniJinja side. This means that it's very intentional that if you pass a dictionary to MiniJinja, the Python .items() method is unavailable.

Here is what this means for some basic types:

  • Python dictionaries and lists (as well as other objects that behave as sequences) appear in the MiniJinja side as native lists. They do not expose any specific other behavior and when they move back to the Python side they will appear as basic lists. Specifically this means that a tuple (which does not exist in MiniJinja) when moving from Python to MiniJinja turns into a list and will remain a list when it moves back.
  • Python objects are represented in MiniJinja similarly to dicts, but they retain all their meaningful Python APIs. This means they stringify via __str__ and they allow the MiniJinja code to call their non-underscored methods. Note that there is no extra security layer in use at the moment so take care of what you pass there.
  • MiniJinja's python binding understand what __html__ is when it exists on a string subclass. This means that a markupsafe.Markup object will appear as safe string in MiniJinja. This information can also flow back to Python again.

Sponsor

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

License and Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minijinja-0.30.2.tar.gz (151.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

minijinja-0.30.2-cp38-abi3-win_amd64.whl (580.6 kB view details)

Uploaded CPython 3.8+Windows x86-64

minijinja-0.30.2-cp38-abi3-win32.whl (563.6 kB view details)

Uploaded CPython 3.8+Windows x86

minijinja-0.30.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (670.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

minijinja-0.30.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (621.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

minijinja-0.30.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (621.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

minijinja-0.30.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (735.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.5+ i686

minijinja-0.30.2-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.3 MB view details)

Uploaded CPython 3.8+macOS 10.9+ universal2 (ARM64, x86-64)macOS 10.9+ x86-64macOS 11.0+ ARM64

File details

Details for the file minijinja-0.30.2.tar.gz.

File metadata

  • Download URL: minijinja-0.30.2.tar.gz
  • Upload date:
  • Size: 151.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.14.10

File hashes

Hashes for minijinja-0.30.2.tar.gz
Algorithm Hash digest
SHA256 6ffb9f4c0e09efae1416a20225fbfc91eab1e100675f451a38876db8b8cfe862
MD5 02638e4b0a866f53cf727f83b37f6a4f
BLAKE2b-256 818fa3a1483d5515d100097b2197dc609e17a91b6254eb72d9cbaa46355c9f2a

See more details on using hashes here.

File details

Details for the file minijinja-0.30.2-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: minijinja-0.30.2-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 580.6 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.14.10

File hashes

Hashes for minijinja-0.30.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9232b1684e9949209457c3db72625c8eff01bf8ce5dfc4a0ae28c11b7d47acfe
MD5 1409107b3b0e9ada64b5c778935067b5
BLAKE2b-256 4621dedbfd2035ba3ea352144920837af300fd5868b7f75a4b55986ee682bb32

See more details on using hashes here.

File details

Details for the file minijinja-0.30.2-cp38-abi3-win32.whl.

File metadata

  • Download URL: minijinja-0.30.2-cp38-abi3-win32.whl
  • Upload date:
  • Size: 563.6 kB
  • Tags: CPython 3.8+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.14.10

File hashes

Hashes for minijinja-0.30.2-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 0e9c4c51cd457820a98daeaa40effd161188df10d0caa657d1e365a91a2b9b9f
MD5 77a3d460a1a254b8de9d02b2ab915d89
BLAKE2b-256 ff7d7aa52c3a80a1e7ae99cf1951beca61f3fe35d4521f537046ec9e08a0bcc6

See more details on using hashes here.

File details

Details for the file minijinja-0.30.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for minijinja-0.30.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 02d68032b1eaba190ba6a93c20111f7477841a6628a4fe4a2c85dcc872ae0c05
MD5 9a6de770215e49d55621068cea40297b
BLAKE2b-256 223bb8a0c9871c8df51b2ecb483cc7a7230997e47f1f771f0d94a2b30ffeff80

See more details on using hashes here.

File details

Details for the file minijinja-0.30.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for minijinja-0.30.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 687df9f09986e94590951033f0e3ee6de930f8177b0173e330c14289c392689a
MD5 a3b51bc35e4d64e54df029507d560114
BLAKE2b-256 91e524fb3f24a3decd0722fbd045b5c9c2ec7d332dbb6f81cd7fd7280602d175

See more details on using hashes here.

File details

Details for the file minijinja-0.30.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for minijinja-0.30.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 df8f4b1f13514b7c2e5398509214939885a03a13d811b7679cdc3fa626a65618
MD5 e233a43caf69998eee5a7bde3678fe7e
BLAKE2b-256 3895e151b8ced144eeb13f0b7c092ecc8362804b4ae0b892eb5db6119c9f8b45

See more details on using hashes here.

File details

Details for the file minijinja-0.30.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl.

File metadata

File hashes

Hashes for minijinja-0.30.2-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm Hash digest
SHA256 d3a3050697a95a8c2cd9f33b27186fb9b9db374f8375df0e68dfea9bc29be411
MD5 8536a556384852b7162800071f122a3f
BLAKE2b-256 dd2ad8a49b103dbcc3bfa7b6243cf7f8308ddc2a81a5b96288d882b4872f1ad7

See more details on using hashes here.

File details

Details for the file minijinja-0.30.2-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for minijinja-0.30.2-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 64cb1f19f8fb648d2a2c57909cdcd8079aa7281a206b24aba1a29bc150aff45f
MD5 d7d9bc6d3d57eea26e093eb145f3686f
BLAKE2b-256 32731373fe08ab67a63f41aa99f8ea898f9da2b4dc489e8e540bd01989dfb908

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page