Skip to main content

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 using Dinja.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


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 hashes)

Uploaded Source

Built Distribution

dinja-0.1.1-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

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