Skip to main content

Jinja2 Custom Filters

Jinja2 offers many filters to use in your template. But this is limited. This repo offers a number of additional filters that can be used in Jinja2 templates.

How it works

  1. The first and foremost is installation.

     pip install jinja2_custom_filters_extension
    
  2. Add extension to your jinja environment.

     from jinja2_custom_filters_extension.string_filters_extension import StringFilterExtension
     environment: Environment = Environment(extensions=[StringFilterExtension])
    
  3. Use the filters that are available in the extension as you would use a built in filter in your jinja templates.
    for ex:

     {{ "abc DEF" | upper_case_first_letter }}
    
  4. Render your template , the filters should be working.

Extensions

String Filter Extension

Filter Name Description Example Input Value Generated Output Value
slug Changes the value to lower case and then replaces spaces with dashes aBc DeF GHi abc-def-ghi
upper_case_first_letter Changes only the first letter of the value to upper case. AbC DeF abC DeF

Usage with cookiecutter

cookiecutter is a famous tool for creating projects from project templates.
Itis built on top of jinja2.
These extensions can also be used in a cookiecutter project.

  1. Make sure the package jinja2_custom_filters_extension is installed.

  2. Add extensions in the cookiecutter.json file of your project.
    Refer the link on how to do it. Bascially it is adding the below entry to the json.

    "_extensions": ["jinja2_custom_filters_extension.string_filters_extension.StringFilterExtension"]
    
  3. Use the filters in your templates.

  4. Thats it. When you run your cookiecutter project, it should work.

Metadata

Release files for jinja2-custom-filters-extension 0.0.2

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-custom-filters-extension 0.0.2
File Size Uploaded
jinja2_custom_filters_extension-0.0.2.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jinja2-custom-filters-extension 0.0.2
File Interpreter ABI Platform
jinja2_custom_filters_extension-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 7.6 kB

Release files / jinja2_custom_filters_extension-0.0.2.tar.gz

Download URL jinja2_custom_filters_extension-0.0.2.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b3fa6e574fb7b07cb4bb40a8e2639fc755c7ad2e9879d279a30d65c299df5edc
BLAKE2b-256 checksum
How to use checksums
1120bf931e08c0731d0927e5fa66b339f679381dd3f772c77da1cb766c0ee339
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

Release files / jinja2_custom_filters_extension-0.0.2-py3-none-any.whl

Download URL jinja2_custom_filters_extension-0.0.2-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
496310ec5db4da7255d0ac725b309528e708c8bdadc538bc0b1e6f95a2519eca
BLAKE2b-256 checksum
How to use checksums
ca9dd725f77e88fd1ff49d9882371e7250ef253a6c4512ef13054e8128e6d02b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

Release history Release notifications | RSS feed

This release

0.0.2 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