Skip to main content

Overview

Jinja2 extension to enable the use of any filter as a custom autoscape filter.

This package allows you to define rules to determine when your custom autoescaping filter will be enabled using the same 'select_autoescape' method you already use to determine when the built-in autoescaping filter is enabled.

Usage

from jinja2_ext_custom_autoescaping import CustomAutoescapeExtension, enable_custom_autoescaping
from jinja2 import Environment, select_autoescape, FileSystemLoader

# Your custom filter...        
def my_filter(val):
    print(val)
    if isinstance(val, str):
        return val.replace(r"\\", r"\\\\")
    return val

    
# Here you set the rules for when the built-in autoescaping will be enabled
built_in_select_autoescape = select_autoescape(enabled_extensions=['html', 'htm', 'xml'],
                                               disabled_extensions=['txt', 'tex'],
                                               default_for_string=True,
                                               default=True)

# - select_autoescape is a closure
# - enabled_extensions takes precedence over disabled_extensions, so an extension in both lists will be enabled
# - You most likely do not want to have custom autoescaping on while built-in autoescaping is also on

# Here you set the rules for when your custom autoescaping will be enabled
custom_select_autoescape = select_autoescape(enabled_extensions=['tex', 'txt'],
                                             disabled_extensions=[],
                                             default_for_string=False,
                                             default=False)

# Just focusing on the important parts of your Environment construction.
env = Environment(extensions=[CustomAutoescapeExtension],
                  loader=FileSystemLoader(['.']),
                  autoescape=built_in_select_autoescape)

opts = {'custom_select_autoescape': custom_select_autoescape,
        'custom_autoescape_filter_name': 'my_filter',
        'custom_autoescape_filter_func': my_filter}

# Register the filter and enables autoescaping
enable_custom_autoescaping(env, **opts)

# Now you are ready to go...
template = env.get_template('test_template.txt')
print(template.render(var={'entry 1': 'value 1', 'entry2': r'val\\ue 2'}))

Metadata

Release files for jinja2-ext-custom-autoescaping 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jinja2-ext-custom-autoescaping 0.1.0
File Size Uploaded
jinja2-ext-custom-autoescaping-0.1.0.tar.gz 2.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jinja2-ext-custom-autoescaping 0.1.0
File Interpreter ABI Platform
jinja2_ext_custom_autoescaping-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.4 kB

Release files / jinja2-ext-custom-autoescaping-0.1.0.tar.gz

Download URL jinja2-ext-custom-autoescaping-0.1.0.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
cf0bfa47b6ee914a7288002502c91e8e995d5ed6ad28f61849ee4ed9da7fb387
BLAKE2b-256 checksum
How to use checksums
d4dfc7de0fc61715d5563ef01f662617400bffc271ac031e0427d4674303a1ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.11 CPython/3.6.7 Linux/4.18.0-13-generic

Release files / jinja2_ext_custom_autoescaping-0.1.0-py3-none-any.whl

Download URL jinja2_ext_custom_autoescaping-0.1.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eea378d0acff57249c76da7f39221886a831ca164ea3a4065465668a14bbfc03
BLAKE2b-256 checksum
How to use checksums
07c8a5ad8411138f9df4ae1af049e6b594a89e951591481f4b22fc996b38356b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.11 CPython/3.6.7 Linux/4.18.0-13-generic

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page