Jinja2 extension that never undefined
Project description
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.j2
with the following content:{% macro test(a, b, c) %} a macro with three parameters but not used {%endmacro%} {{test()}}
-
Create a Python script named
example.py
with the following content:from jinja2 import Environment, FileSystemLoader from your_package_name 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:
python example.py
The script will raise an exception because parameters a b c
are not provided when calling macro test
in the template.
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.
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
Hashes for jinja2_neverundefined-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcf144730e9d7cf497d8b763a02e66ebc1747a2b9b973065717027dbeff21c88 |
|
MD5 | 0a4030c33cf5bd223a30b86a960bf60c |
|
BLAKE2b-256 | ea4e162731c16697290dc8aa545c5358d3dc66d2ca57df0b0ea95f93aa6a40d3 |