Base classes for quick-and-easy template tag development
Low maintenance activity. Last release over 2 years ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install jinja2-simple-tagsjinja2-simple-tags
jinja2-simple-tags is a library that provides a simple way to create custom template
tags in Jinja2 templates.
Compatibility
python>= 3.6Jinja2>= 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()