Skip to main content

Library functionality for Jinja2 extensions

Project description

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.

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_library-0.5.1.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jinja2_library-0.5.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file jinja2_library-0.5.1.tar.gz.

File metadata

  • Download URL: jinja2_library-0.5.1.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for jinja2_library-0.5.1.tar.gz
Algorithm Hash digest
SHA256 6ae80dbf25ae15c278e20f24db9fecce075e378d31e83871f36624be07730962
MD5 b6798efc9f87c37c51ed794c443ca66f
BLAKE2b-256 96720a85c6d7f6f247e14f551659deac1758b3cffa7f3c43e1d4f168c1ce3772

See more details on using hashes here.

Provenance

The following attestation bundles were made for jinja2_library-0.5.1.tar.gz:

Publisher: release-to-pypi.yml on jenisys/jinja2-library

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jinja2_library-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: jinja2_library-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for jinja2_library-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b3259bda08d7cd72628761d521682c04899a3ade8d73f7fd41627583c75577a8
MD5 74d4fde9c0103b18751afbc7be6db047
BLAKE2b-256 c7c50fa7926cb38e18b207cc0b7ddaa56b0046670710996b34f6852abdff3f46

See more details on using hashes here.

Provenance

The following attestation bundles were made for jinja2_library-0.5.1-py3-none-any.whl:

Publisher: release-to-pypi.yml on jenisys/jinja2-library

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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