Skip to main content

Library for Jinja2 Template Extensions

CI Build Status Latest Version License

Provides a library functionality for Jinja2 extensions that can be used in templates.

FEATURES:

  • Provides a library concept for Jinja2 extensions, like filters, tests, globals and directives (aka: extensions),
  • Declarative approach to describe and register Jinja2 extensions.
  • Simplifies how library items are registered in an Environment (in one step).
  • Supports own namespace/scope for each library.
  • Supports control over registered names.

EXAMPLE 1: Basic Example

# -- FILE: example/hello.py
from jinja2_library import Library
import jinja2.ext

this_library = Library()
register = this_library.make_register_decorator()

@register.filter
def hello(value, greeting=None):
    greeting = greeting or "Hello"
    return u"{greeting} {name}".format(greeting=greeting, name=value)

class LibraryExtension(jinja2.ext.Extension):
    """Simplifies to use ``this_library`` as Jinja2 extension."""
    def __init__(self, environment):
        super(LibraryExtension, self).__init__(environment)
        this_library.add_to_environment(environment)

The template library can be used by using the LibraryExtension class in Jinja2:

# -- FILE: use_template_library_hello.py
# USING: example.hello.LibraryExtension
from jinja2 import Environment
from assertpy import assert_that

this_extension = "example.hello.LibraryExtension"
this_template = "HELLO: {{ this.name|hello(greeting=this.greeting) }}"
this_data = dict(name="Alice", greeting="Ciao")

environment = Environment(extensions=[this_extension])
template = environment.from_string(this_template)
this_text = template.render(this=this_data)
assert_that(this_text).is_equal_to("HELLO: Ciao Alice")

API

Library class

The Library constructor provides the following parameter:

Parameter Type Default Description
scope Optional[string] None Scope/namespace for registered items.
  • The scope parameter allows to provide a namespace for the registered Library items.
  • This prevents name collisions if many filters/tests/globals are used.
  • Simplifies the registration of filters/tests/globals/extensions into an Environment.

EXAMPLE:

# -- FILE: example/scoped_hello.py
from jinja2_library import Library
import jinja2.ext

this_library = Library("foo.bar")  # -- HINT: Library with scope="foo.bar".
register = this_library.make_register_decorator()

@register.filter(name="hello")
def hello_filter(value, greeting=None):
    greeting = greeting or "Hello"
    return u"{greeting} {name}".format(greeting=greeting, name=value)

class LibraryExtension(jinja2.ext.Extension):
    def __init__(self, environment):
        super(LibraryExtension, self).__init__(environment)
        this_library.add_to_environment(environment)

Scoped library names can then be used inside a template, like:

{#- FILE: template.jinja -#}
{#- USING: extensions=["example.scoped_hello.LibraryExtension"] -#}
HELLO: {{ this.name|foo.bar.hello(greeting="Ciao") }}

Register Decorators

This Python package provides the following decorators (in the context of the example above):

Register Decorator Description
@register.filter Register a Jinja2 filter function to this_library.
@register.test Register a Jinja2 test predicate function to this_library.
@register.global_ Register a Jinja2 global function or variable to this_library.
@register.extension Register a [Jinja2 extension] class (aka: jinja2.ext.Extension).

The function decorators for @register.filter/test/global_ support optional parameters. These optional decorator parameters are:

Parameter Type Default Description
name string {function.name} Name of the filter/test/global.
aliases sequence<string> EMPTY_SEQUENCE List of alternative names.

Background Info

Inspired by the Django template library approach.

History

  • INITIALLY CREATED AS: simplegen.template_lib
  • REFACTORING: Extracted into own standalone package to simplify reuse with Jinja2 template engine.

Metadata

Release files for jinja2-library 0.5.1

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-library 0.5.1
File Size Uploaded
jinja2_library-0.5.1.tar.gz 17.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jinja2-library 0.5.1
File Interpreter ABI Platform
jinja2_library-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 25.3 kB

Release files / jinja2_library-0.5.1.tar.gz

Download URL jinja2_library-0.5.1.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6ae80dbf25ae15c278e20f24db9fecce075e378d31e83871f36624be07730962
BLAKE2b-256 checksum
How to use checksums
96720a85c6d7f6f247e14f551659deac1758b3cffa7f3c43e1d4f168c1ce3772
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2025.

Transparency log

Release files / jinja2_library-0.5.1-py3-none-any.whl

Download URL jinja2_library-0.5.1-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b3259bda08d7cd72628761d521682c04899a3ade8d73f7fd41627583c75577a8
BLAKE2b-256 checksum
How to use checksums
c7c50fa7926cb38e18b207cc0b7ddaa56b0046670710996b34f6852abdff3f46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

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