Base classes for quick-and-easy development of template tags
Project description
jinja2-simple-tags
Base classes for quick-and-easy development of template tags
Installation
pip install jinja2-simple-tags
Examples
StandaloneTag
from django.utils.timezone import now
from django.utils.formats import date_format
from jinja2_simple_tags import StandaloneTag
class NowExtension(StandaloneTag):
tags = {"now"}
def render(self, format_string='DATETIME_FORMAT'):
return date_format(now(), format_string)
Usage:
{% now %} {# 7th July 2020, 10:07 a.m. #}
{% now "Y-m-d" %} {# 2020-07-07 #}
ContainerTag
from django.core.cache import cache
from django.utils.encoding import force_str
from django.core.cache.utils import make_template_fragment_key
from jinja2_simple_tags import ContainerTag
class CacheExtension(ContainerTag):
tags = {"cache"}
def render(self, fragment_name, *vary_on, timeout=None, caller=None):
cache_key = make_template_fragment_key(fragment_name, vary_on)
value = cache.get(cache_key)
if value is None:
value = caller()
cache.set(cache_key, force_str(value), timeout)
else:
value = force_str(value)
return value
Usage:
{% cache "footer", request.path, timeout=3600 %}
<footer>
...
</footer>
{% endcache %}
Assignment
Both StandaloneTag
and ContainerTag
comes with out-of-the-box
support for assignment.
Usage:
{% now "Y-m-d" as today %}
...
{{ today }} {# 2020-07-07 #}
{% cache "footer", request.path, timeout=3600 as footer %}
<footer>
...
</footer>
{% endcache %}
...
{{ footer }}
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
Close
Hashes for jinja2_simple_tags-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d31b1584ec17136cbad6bb1c001979bab5849c45597d7c5f59bdd6ca32a2ee24 |
|
MD5 | a723dab3e6355545c4a0688bd71b202d |
|
BLAKE2b-256 | 7ea1bec7929364b821039e3c8bb8bdcfcc6b15ccd5e340ed4050b78f1c79146d |