Singleton class that provides decorators to create custom `filters` and `tags` for Jinja templates.
Project description
Dinja: Jinja
Tool-Kit
Dinja
is a singleton class that provides decorators to create custom filters
and tags
for Jinja templates. It also allows you to load these filters and tags into a Jinja environment.
Install
pip install dinja Jinja2
Usage
Load
custom filters and tags into a Jinja environment usingDinja.load
.- Create custom
filters
using the@Dinja.filter
decorator. - Create custom
tags
using the@Dinja.tag
decorator.
Method: load
Load custom filters and tags into a Jinja environment.
Parameters:
jinja_env
(jinja2.Environment): The Jinja environment to load into.
Example
from jinja2 import Environment, FileSystemLoader
from dinja import Dinja
dinja = Dinja()
jinja_env = Environment(loader=FileSystemLoader("./templates"))
dinja.load(jinja_env)
Decorator: filter
Decorator to create custom filters for Jinja templates.
Parameters:
method
(function): The filter function.is_safe
(bool): Indicate whether the tag result is safe for HTML rendering (default:False
).
Usage:
from dinja import Dinja
dinja = Dinja()
@dinja.filter # dinja.filter(is_safe=True)
def my_filter(value):
return "Filtered: " + value.lower()
<div>{{ "Some-Value" | my_filter }}</div>
Returns:
Filter
: The wrapped filter function.
Decorator: tag
Decorator to create custom tags for Jinja templates.
Parameters:
method
(function): The tag function.mode
(str): Jinja tag's current mode, options: {simple
,value
,content
} (default:simple
).is_safe
(bool): Indicate whether the tag result is safe for HTML rendering (default:False
).
Usage:
from dinja import Dinja
dinja = Dinja()
@dinja.tag # dinja.tag(mode="value", is_safe=True)
def simple_tag():
return "Simple Tag"
@dinja.tag(mode="value")
def my_value_tag(value):
return '<h1>' + value + '</h1>'
@dinja.tag(mode="content")
def my_content_tag(content):
return content
<!-- @Example: { simple_tag } -->
{% simple_tag %}
<!-- @Example: { my_value_tag } -->
{% my_value_tag "some_value" %}
<!-- @Example: { my_content_tag } -->
{% my_content_tag %}
<div>Custom Content</div>
{% endmy_content_tag %}
Returns:
Extension
: The custom Jinja2 extension class.
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
dinja-0.1.1.tar.gz
(4.7 kB
view details)
Built Distribution
dinja-0.1.1-py3-none-any.whl
(4.4 kB
view details)
File details
Details for the file dinja-0.1.1.tar.gz
.
File metadata
- Download URL: dinja-0.1.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cf4ff0d0db3307fe91f128643b0301f717c324c5ff3a4ea0f3ba2a4abc2e964 |
|
MD5 | 914b77885e2c996d94848cb0b3243632 |
|
BLAKE2b-256 | 636fa506288b98c73a387637885a7f01e89f78769541233a65a1d65c32e1f8a4 |
File details
Details for the file dinja-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: dinja-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01997012c538813e1c8528432cf5545c0164c96215cbef77c8e34ef50e9d343c |
|
MD5 | 3ca10354c355c72b38abfbbdda9f92d5 |
|
BLAKE2b-256 | 9422e8c6ccca759442726c94a5705e09fddfdaa5367c9140cfc7f6170cd28469 |