Jinja2 extension adding {% try %} {% catch %} exception handling
Project description
jinja-try-catch
A Jinja2 extension providing exception handling within templates.
{%- try -%}
{{ 1 / 0 }}
{%- catch -%}
Can't divide by zero!
{%- endtry -%}
Can't divide by zero!
This extension works in both sync and async environments, as well as the native variants, too.
Quickstart
pip install jinja-try-catch
And add the TryCatchExtension
to your Environment
extensions list:
import jinja2
from jinja_try_catch import TryCatchExtension
jinja_env = jinja2.Environment(extensions=[TryCatchExtension])
Usage
Suppressing errors
Simply omit the {% catch %}
to silently swallow exceptions
{%- try -%}
{{ 1 / 0 }}
{%- endtry -%}
Error messages
Define a {% catch %}
to render something else if an exception is raised
{%- try -%}
{{ 1 / 0 }}
{%- catch -%}
There are infinite zeroes, duh
{%- endtry -%}
There are infinite zeroes, duh
Display exceptions
The raised exception is exposed within the {% catch %}
through the {{ exception }}
variable
{%- try -%}
{{ 1 / 0 }}
{%- catch -%}
Uh-oh, an error occurred:
{{ exception.__class__.__name__ }}: {{ exception }}
{%- endtry -%}
Uh-oh, an error occurred:
ZeroDivisionError: division by zero
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
jinja_try_catch-0.1.0.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for jinja_try_catch-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ca0c5f93acd5162656a6d49479c4c3ec37bb4f1cb2ae192ea6818d8063e933f |
|
MD5 | a08197ad3dfb053881c54cf6cb1861e5 |
|
BLAKE2b-256 | 1184123ae467c0054bd0533c1e533a5431f465a12734e785eff2395ba97a18ea |