{"id":2547,"library":"jinja2-simple-tags","title":"Jinja2 Simple Tags","description":"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.","status":"active","version":"0.6.1","language":"en","source_language":"en","source_url":"https://github.com/dldevinc/jinja2-simple-tags","tags":["jinja2","templating","custom-tags","extension","frontend","web-development"],"install":[{"cmd":"pip install jinja2-simple-tags","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core templating engine this library extends.","package":"Jinja2","optional":false},{"reason":"Requires Python 3.6 or higher.","package":"python","optional":false}],"imports":[{"note":"Used for tags that do not require a closing tag.","symbol":"StandaloneTag","correct":"from jinja2_simple_tags import StandaloneTag"},{"note":"Used for tags that require a closing tag and can contain content.","symbol":"ContainerTag","correct":"from jinja2_simple_tags import ContainerTag"},{"note":"Used for tags that include other templates.","symbol":"InclusionTag","correct":"from jinja2_simple_tags import InclusionTag"}],"quickstart":{"code":"from jinja2 import Environment\nfrom jinja2_simple_tags import StandaloneTag\nfrom datetime import datetime\n\n# 1. Define your custom standalone tag\nclass CurrentTimeTag(StandaloneTag):\n    tags = {\"now\"}\n\n    def render(self, format_string=\"%Y-%m-%d %H:%M:%S\"): # Renamed 'format' to 'format_string' to avoid conflict with built-in format\n        return datetime.now().strftime(format_string)\n\n# 2. Set up Jinja2 Environment and load your extension\nenv = Environment(\n    extensions=[CurrentTimeTag]\n)\n\n# 3. Create a template string using your custom tag\ntemplate_content = \"\"\"\n<p>The current date and time is: {% now %}</p>\n<p>Formatted time: {% now '%H:%M' %}</p>\n\"\"\"\ntemplate = env.from_string(template_content)\n\n# 4. Render the template\noutput = template.render()\nprint(output)","lang":"python","description":"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."},"warnings":[{"fix":"Set `safe_output = True` as a class attribute on your tag class, or return a `jinja2.Markup` object from your `render` method.","message":"Output 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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)`).","message":"Jinja2-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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}