An experimental Python binding of the Rust MiniJinja template engine.
Project description
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"
})
# alternatively
def loader(template_name):
if template_name == "template_name":
return "Template soruce"
return None
env = Environment(loader=loader)
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)
Utility
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.
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 amarkupsafe.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for minijinja-0.30.0-cp38-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe86af76b16ff5d7935f8470b5bd99565634307347f973e0d7f0685d4684280a |
|
MD5 | 5514b020b2dd5a7b4154cc34f129bbc2 |
|
BLAKE2b-256 | 399ef85c28578ea947697e127551e34e7708a8f21f73820d0c45db1f8a99ed64 |
Hashes for minijinja-0.30.0-cp38-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d16aa47c5e367586e8be936d86d3479d8f2f77e96ec7055ed1be7255bfab8509 |
|
MD5 | ab43a2c07966f9e6dc5d015bbf7655ba |
|
BLAKE2b-256 | 88c656fc65d0123cf578732eb1fbb9766ec9d7c2b21250247023e0ac0ffbc9ff |
Hashes for minijinja-0.30.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0b83a01d040652f25c830061be279e59d7fe1f45623a60d4542cdd9a9160324 |
|
MD5 | 2923b3ae9c3b993af6bdd8b69ff70240 |
|
BLAKE2b-256 | 587924c20d58a255730de00e67bd763c7c0923d1eca11d4bcab3f9311bbfc621 |
Hashes for minijinja-0.30.0-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fa27f2bb6746215befcb8ad2fdd1f3265073b98168d9f21a8db29cf5fc44c4a |
|
MD5 | 685060f42303c75e057d51bd81f0187e |
|
BLAKE2b-256 | 6654526fd38b7ecfc8a51aa11527fedeb7d3720d4a82c489e33cae639530981f |
Hashes for minijinja-0.30.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1f09b971c3545a274f841b131bf5b770c0e66eaad4f296e83b18c4368950f8e |
|
MD5 | 37f17956728e6fc1592c02f0c3431980 |
|
BLAKE2b-256 | b6304f02abf903d9c9ef448b1b3a58c65b656b8024461d65f74031da9784ddf7 |
Hashes for minijinja-0.30.0-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fce090715e11d81e88f7ebe1347ca184585d55b2419434d48ed4920aa76d652d |
|
MD5 | 049288a2ea07fbe547b61397c8c6cb4e |
|
BLAKE2b-256 | 906381c728abda48b342188257e3f3ca5154691d900b6b89121b07fdb78db5e6 |
Hashes for minijinja-0.30.0-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35a69966c0b986fde260cdac6eb97628676d752af14671f271c56a7ad6620914 |
|
MD5 | d3e2d7a8d1b676dff1c3a9b784bcd4bd |
|
BLAKE2b-256 | 6325f28179c663099e9a93d8a10b5adcc8446108bf1e442cddb67132b6ccbec9 |