Registry / web-framework / jinja2-simple-tags

jinja2-simple-tags

JSON →
library0.6.1pypypi✓ verified 24d ago

jinja2-simple-tags is a Python library that simplifies the creation of custom template tags within Jinja2 templates. It provides base classes like StandaloneTag, ContainerTag, and InclusionTag, allowing developers to extend Jinja2's functionality with Python code. The current version is 0.6.1, and it maintains an active release cadence.

pip install jinja2-simple-tags
INSTALL
IMPORT
SIG · JINJA2-SIMPLE-TAGS
J
jinja2-simple-tags
web-frameworkpythonv0.6.1
Install
1.8s avg
Import
146ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.6.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.152s · 18.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.140s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

StandaloneTag
from jinja2_simple_tags import StandaloneTag
Used for tags that do not require a closing tag.
ContainerTag
from jinja2_simple_tags import ContainerTag
Used for tags that require a closing tag and can contain content.
InclusionTag
from jinja2_simple_tags import InclusionTag
Used for tags that include other templates.

This quickstart demonstrates how to define a simple custom tag using StandaloneTag, register it with a Jinja2 environment, and render a template that uses the new tag to display the current time. Remember to pass your custom tag class to the `extensions` list of the Jinja2 Environment.

from jinja2 import Environment from jinja2_simple_tags import StandaloneTag from datetime import datetime # 1. Define your custom standalone tag class CurrentTimeTag(StandaloneTag): tags = {"now"} def render(self, format_string="%Y-%m-%d %H:%M:%S"): # Renamed 'format' to 'format_string' to avoid conflict with built-in format return datetime.now().strftime(format_string) # 2. Set up Jinja2 Environment and load your extension env = Environment( extensions=[CurrentTimeTag] ) # 3. Create a template string using your custom tag template_content = """ <p>The current date and time is: {% now %}</p> <p>Formatted time: {% now '%H:%M' %}</p> """ template = env.from_string(template_content) # 4. Render the template output = template.render() print(output)
Debug
Known issues
gotchaOutput from custom tags (especially StandaloneTag) is HTML-escaped by default. If your tag generates raw HTML or script that should not be escaped, you must explicitly mark it as safe.
fix
Set `safe_output = True` as a class attribute on your tag class, or return a `jinja2.Markup` object from your `render` method.
affects: All versions
gotchaWhen using `InclusionTag`, be mindful of context inheritance. By default, it inherits the parent template's context. Explicitly manage context variables to avoid unexpected behavior or data leakage.
fix
The `get_context()` method allows you to merge additional context variables. Understand Jinja2's context behavior (e.g., `with context` or `without context` in `include` statements) when designing your inclusion tags.
affects: All versions
gotchaJinja2-simple-tags relies on Jinja2's parsing. Common Jinja2 `TemplateSyntaxError` issues (e.g., unclosed tags, misspelled keywords, mismatched delimiters within the custom tag's arguments or content) will still apply and can be challenging to debug.
fix
Carefully review your template syntax and the arguments passed to your custom tags. Enable Jinja2's strict undefined behavior for earlier detection of missing variables during development (`Environment(undefined=StrictUndefined)`).
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'jinja2_simple_tags'
The `jinja2-simple-tags` library has not been installed in your Python environment or is not accessible within the current environment.
fix
pip install jinja2-simple-tags
ModuleNotFoundError: No module named 'jinja2'
The core Jinja2 library, a dependency of `jinja2-simple-tags`, is not installed or available in your Python environment.
fix
pip install Jinja2
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'mytag'
A custom tag defined using `jinja2-simple-tags` was used in a template, but the tag was not properly registered with the Jinja2 environment, or there's a typo in the tag name.
fix
Ensure your custom tag class is added to the Jinja2 environment's extensions. Example: `env = Environment(extensions=[MyCustomTagExtension])` or `env.add_extension(MyCustomTagExtension)`.
jinja2.exceptions.UndefinedError: 'None' has no attribute 'some_attribute'
An attempt was made to access an attribute or item on a variable that is `None` (or undefined) within the Jinja2 template, often occurring when data passed to the template or accessed within a custom tag is missing or unexpectedly null.
fix
Check if the variable or its attribute exists before trying to access it, for example, using an `if` condition: `{% if my_object and my_object.some_attribute %}{{ my_object.some_attribute }}{% endif %}` or by providing a default value: `{{ my_object.some_attribute | default('N/A') }}`.
Upgrade
Version history
0.6.1latest on PyPI · released Mar 6, 2024
Audit
Dependencies
Jinja2requiredCore templating engine this library extends.
pythonrequiredRequires Python 3.6 or higher.
Agent activity
7 hits · last 30 days
node
6
Resources
jinja2-simple-tags — pip install jinja2-simple-tags · libregistry