Skip to main content

jinja2-simple-tags

jinja2-simple-tags is a library that provides a simple way to create custom template tags in Jinja2 templates.

PyPI Build Status

Compatibility

  • python >= 3.6
  • Jinja2 >= 2.10

Installation

pip install jinja2-simple-tags

Usage

To use jinja2-simple-tags, you need to create a subclass of one of the provided tag types and implement the render method.

StandaloneTag

StandaloneTag is a tag that doesn't require a closing tag. It can be used like this:

from datetime import datetime
from jinja2_simple_tags import StandaloneTag


class NowExtension(StandaloneTag):
    tags = {"now"}

    def render(self, format="%Y-%m-%d %H:%I:%S"):
        return datetime.now().strftime(format)
{% now %}               {# 2023-04-27 20:08:03 #}
{% now '%m/%d/%Y' %}    {# 04/27/2023 #}

Escaping

By default, the output of StandaloneTag will be escaped. To disable escaping, set the safe_output property of your tag to True:

from jinja2_simple_tags import StandaloneTag


class AlertExtension(StandaloneTag):
    safe_output = True
    tags = {"alert"}

    def render(self, message):
        return "<script>alert('{}')</script>".format(message)

You can also return a jinja2.Markup object from the render() method to explicitly mark the output as safe.

ContainerTag

ContainerTag is a tag that requires a closing tag and can contain arbitrary content. It can be used like this:

import hmac
from jinja2_simple_tags import ContainerTag


class HMACExtension(ContainerTag):
    tags = {"hmac"}

    def render(self, secret, digest="sha256", caller=None):
        content = str(caller()).encode()

        if isinstance(secret, str):
            secret = secret.encode()

        signing = hmac.new(secret, content, digestmod=digest)
        return signing.hexdigest()
{% hmac 'SECRET', digest='sha1' %}Hello world!{% endhmac %}

{# e29371e24dc99c5641681728855a92e26829e288 #}

InclusionTag

InclusionTag is a tag that can be used for including other templates. It allows you to specify a template name or implement the get_template_names() method for dynamic template selection. Here's an example:

from jinja2_simple_tags import InclusionTag

class IncludeHeader(InclusionTag):
    tags = {"include_header"}
    template_name = "header.html"

    def get_context(self, logo):
        return {
            "logo": logo
        }
{% include_header logo="/static/logo.png" %}

Context Inheritance

InclusionTag inherits the current context from the parent template, which allows you to access and use variables from the parent context within the included template. Any additional context variables returned by the get_context() method are merged with the inherited context.

Context

Current context can be accessed using self.context attribute of the tag class:

from jinja2_simple_tags import StandaloneTag


class UserNameExtension(StandaloneTag):
    tags = {"username"}

    def render(self):
        return self.context["user"].username

Assignment

In addition to returning the rendered value, ContainerTag, StandaloneTag and InclusionTag also supports assigning the output to a variable in the context. This can be done using the as keyword:

{% now '%m/%d/%Y' as today %}    
...
{{ today }}         {# 04/27/2023 #}
{% hmac 'SECRET', digest='sha1' as signature %}Hello world!{% endhmac %}
...
{{ signature }}     {# e29371e24dc99c5641681728855a92e26829e288 #}

Metadata

Release files for jinja2-simple-tags 0.6.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-simple-tags 0.6.1
File Size Uploaded
jinja2-simple-tags-0.6.1.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jinja2-simple-tags 0.6.1
File Interpreter ABI Platform
jinja2_simple_tags-0.6.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 15.7 kB

Release files / jinja2-simple-tags-0.6.1.tar.gz

Download URL jinja2-simple-tags-0.6.1.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
54abf83883dcd13f8fd2ea2c42feeea8418df3640907bd5251dec5e25a6af0e3
BLAKE2b-256 checksum
How to use checksums
d8b7a2c8a0c60e6f2cb0dbc5020b43df7b0dc9a8fae0767c2f72665a1c7b76c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.12

Release files / jinja2_simple_tags-0.6.1-py2.py3-none-any.whl

Download URL jinja2_simple_tags-0.6.1-py2.py3-none-any.whl
Size 5.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7b7cfa92f6813a1e0f0b61b9efcab60e6793674753e1f784ff270542e80ae20f
BLAKE2b-256 checksum
How to use checksums
78058fd074379bdfeb5ce1bccf4a8e23e004e1f238938724f743267759da94e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.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