Library functionality for Jinja2 extensions
Project description
Library for Jinja2 Template Extensions
Provides a library functionality for Jinja2 extensions that can be used in templates.
FEATURES:
- Provides a
libraryconcept for Jinja2 extensions, like filters, tests, globals and directives (aka:extensions), - Declarative approach to describe and register Jinja2 extensions.
- Simplifies how
libraryitems are registered in anEnvironment(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
scopeparameter allows to provide a namespace for the registeredLibraryitems. - 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ae80dbf25ae15c278e20f24db9fecce075e378d31e83871f36624be07730962
|
|
| MD5 |
b6798efc9f87c37c51ed794c443ca66f
|
|
| BLAKE2b-256 |
96720a85c6d7f6f247e14f551659deac1758b3cffa7f3c43e1d4f168c1ce3772
|
Provenance
The following attestation bundles were made for jinja2_library-0.5.1.tar.gz:
Publisher:
release-to-pypi.yml on jenisys/jinja2-library
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jinja2_library-0.5.1.tar.gz -
Subject digest:
6ae80dbf25ae15c278e20f24db9fecce075e378d31e83871f36624be07730962 - Sigstore transparency entry: 381856723
- Sigstore integration time:
-
Permalink:
jenisys/jinja2-library@e41c350d3c69278e9cf2fba9a2f5c0d00d9906a3 -
Branch / Tag:
refs/tags/v0.5.1 - Owner: https://github.com/jenisys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-to-pypi.yml@e41c350d3c69278e9cf2fba9a2f5c0d00d9906a3 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3259bda08d7cd72628761d521682c04899a3ade8d73f7fd41627583c75577a8
|
|
| MD5 |
74d4fde9c0103b18751afbc7be6db047
|
|
| BLAKE2b-256 |
c7c50fa7926cb38e18b207cc0b7ddaa56b0046670710996b34f6852abdff3f46
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jinja2_library-0.5.1-py3-none-any.whl -
Subject digest:
b3259bda08d7cd72628761d521682c04899a3ade8d73f7fd41627583c75577a8 - Sigstore transparency entry: 381856737
- Sigstore integration time:
-
Permalink:
jenisys/jinja2-library@e41c350d3c69278e9cf2fba9a2f5c0d00d9906a3 -
Branch / Tag:
refs/tags/v0.5.1 - Owner: https://github.com/jenisys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-to-pypi.yml@e41c350d3c69278e9cf2fba9a2f5c0d00d9906a3 -
Trigger Event:
release
-
Statement type: