Skip to main content

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

  1. Install the package:

    pip3 install jinja2_neverundefined
    
  2. 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()}}
    
  3. Create a Python script named example.py with 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)
    
  4. 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

jinja2_neverundefined-0.4.0.tar.gz (2.4 kB view details)

Uploaded Source

File details

Details for the file jinja2_neverundefined-0.4.0.tar.gz.

File metadata

  • Download URL: jinja2_neverundefined-0.4.0.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for jinja2_neverundefined-0.4.0.tar.gz
Algorithm Hash digest
SHA256 ee0d918a9479abbefaf8c0582d5e397c5b1a87d1501ed7955312a2e68d0d36ef
MD5 471539b05f9c436fe9a77fe5498acd9f
BLAKE2b-256 e6ee05541fd8545570f89242d079528d5ae860067d7e6898305c34aafe87cd76

See more details on using hashes here.

Provenance

Supported by

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