Jinja: NeverUndefined
End the lawless era of Jinja.
NeverUndefined is more restrictive than StrictUndefined. See this issue.
Usage
-
Install the package:
pip3 install jinja2_neverundefined
-
Create a template file named
template.j2with the following content:{% macro test(a, b, c) %} a macro with three parameters but not used {%endmacro%} {{test()}}
-
Create a Python script named
example.pywith the following content:from jinja2 import Environment, FileSystemLoader from jinja2_neverundefined import NeverUndefined # Create Jinja2 environment with the extension env = Environment( loader=FileSystemLoader('.'), undefined=NeverUndefined ) # Render the template template = env.get_template('template.j2') rendered = template.render() # Print the rendered output print(rendered)
-
Run the script:
python3 example.py
The script will raise an exception because parameters a b c are not provided when calling macro test in the template.
Take another example:
{% macro mtest(a, b, c) %}
append undefined parameters to list
{% set s = [] %}
{% do s.append(a) %}
{{ s }}
{% endmacro %}
{{ mtest() }}
StrictUndefined will not throw error. It will produce:
append undefined parameters to list
[Undefined]
However, NeverUndefined will:
File "template.j2", line 9, in top-level template code
{{ mtest() }}
└ <Macro 'mtest'>
File "/usr/local/lib/python3.9/dist-packages/jinja2/runtime.py", line 777, in _invoke
rv = self._func(*arguments)
│ └ [missing, missing, missing]
└ <Macro 'mtest'>
File "template.j2", line 2, in template
{% macro mtest(a, b, c) %}
File "/usr/local/lib/python3.9/dist-packages/jinja2_neverundefined/__init__.py", line 14, in __init__
raise Exception(info)
└ "parameter 'a' was not provided"
Exception: parameter 'a' was not provided
By using the NeverUndefined extension, the Jinja environment will raise an exception when encountering an undefined variable. This helps catch potential issues or missing variable bindings in your templates.
Metadata
Release files for jinja2-neverundefined 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jinja2_neverundefined-0.6.0.tar.gz | 2.7 kB | Without provenance | Details for jinja2_neverundefined-0.6.0.tar.gz |
Release files / jinja2_neverundefined-0.6.0.tar.gz
| Download URL | jinja2_neverundefined-0.6.0.tar.gz |
|---|---|
| Size | 2.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
16201ed940e86fe42f1312559e47a26d5490d9aa4dc2f03a52544da57e5ae781
|
|
BLAKE2b-256 checksum How to use checksums |
d057f562d967ed17308269f69583c8db0bd0ebbca56df02a41be9a2602f08636
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.12
|
Security for jinja2-neverundefined 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.