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"
})
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 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.3-cp38-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c734ca27714df9e4335afa2e05811e6607436aa95f7eadc1fd85c3fd6ce23c1 |
|
MD5 | ed6e8dada6b2e843dd210ea0119edfcc |
|
BLAKE2b-256 | 1127d3b97b23f41bd8f3b30c60c0e18824f0a69e53e2af0a1fecaa9de0679aa6 |
Hashes for minijinja-0.30.3-cp38-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a6e6f92371216814b14983aad2874cd6678f5cac07f3096f8cabd3abe9bf965 |
|
MD5 | c00153074dbe525d984e85a25171f569 |
|
BLAKE2b-256 | 161c7e8f22ce9769c5547842460f0ab31d7fe0c9bd26439217425ed6b0b4cba1 |
Hashes for minijinja-0.30.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60478e5a1d946d57195d01554b6072817ec00916f862e648eb5d199f5e0a5de2 |
|
MD5 | d78d97c3d8600804f04870b45fe6dd76 |
|
BLAKE2b-256 | b807428d404ae56c3e57431d7e9f80e6c709e90e796cfd9156bab0352b221465 |
Hashes for minijinja-0.30.3-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7f1993553c17a1000a411f40a4a27300e4df35b512d4c3087d7bb83af5f58ae |
|
MD5 | d4b6ad87dcccaa2ba552ded118416c15 |
|
BLAKE2b-256 | b9a7cb14cb4e23d88227d17a759dd13b58ab8f2af85824b200c4c39e0a6488a2 |
Hashes for minijinja-0.30.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94eca7aff7825ab257ed966f255ce885a33e87ac53a4447917acc74c294129d5 |
|
MD5 | 86ea0fe9a9dda88f6fd604de0fdabc25 |
|
BLAKE2b-256 | a5cf4fabb4d43eb224be054fdacea6b88ecc67627a29ef9e58794ea596a263ac |
Hashes for minijinja-0.30.3-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45b18d63730b2468f308370b29f1433c9380bc12d85846871a70f8292ee6cf24 |
|
MD5 | 49a8507d9ef44c288be8e71985fb752e |
|
BLAKE2b-256 | 1b8ee529678a419e6b601e566e7c6573a055a7383adc8fa70164de9e15eb1224 |
Hashes for minijinja-0.30.3-cp38-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 177d74e6e19cd343ea58797b285b710dac8c06c9c5ef8c02916d58c7df4dd2d0 |
|
MD5 | 95b415406f8af0a8befd8eb010066264 |
|
BLAKE2b-256 | 560f84a48afaee71bb456204242204c3cd33df5cb1d4b0b3c624a063ead90640 |