# jinja2-simple-tags
`jinja2-simple-tags` is a library that provides a simple way to create custom template
tags in Jinja2 templates.
[](https://pypi.org/project/jinja2-simple-tags/)
[](https://travis-ci.org/dldevinc/jinja2-simple-tags)
## 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:
```python
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)
```
```jinja2
{% 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`:
```python
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:
```python
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()
```
```jinja2
{% 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:
```python
from jinja2_simple_tags import InclusionTag
class IncludeHeader(InclusionTag):
tags = {"include_header"}
template_name = "header.html"
def get_context(self, logo):
return {
"logo": logo
}
```
```jinja2
{% 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:
```python
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:
```jinja2
{% now '%m/%d/%Y' as today %}
...
{{ today }} {# 04/27/2023 #}
```
```jinja2
{% hmac 'SECRET', digest='sha1' as signature %}Hello world!{% endhmac %}
...
{{ signature }} {# e29371e24dc99c5641681728855a92e26829e288 #}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/dldevinc/jinja2-simple-tags",
"name": "jinja2-simple-tags",
"maintainer": "Mihail Mishakin",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "x896321475@gmail.com",
"keywords": "",
"author": "Mihail Mishakin",
"author_email": "x896321475@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/d8/b7/a2c8a0c60e6f2cb0dbc5020b43df7b0dc9a8fae0767c2f72665a1c7b76c9/jinja2-simple-tags-0.6.1.tar.gz",
"platform": "OS Independent",
"description": "# jinja2-simple-tags\n\n`jinja2-simple-tags` is a library that provides a simple way to create custom template \ntags in Jinja2 templates.\n\n[](https://pypi.org/project/jinja2-simple-tags/)\n[](https://travis-ci.org/dldevinc/jinja2-simple-tags)\n\n## Compatibility\n\n- `python` >= 3.6\n- `Jinja2` >= 2.10\n\n## Installation\n\n`pip install jinja2-simple-tags`\n\n## Usage\n\nTo use `jinja2-simple-tags`, you need to create a subclass of one of the provided \ntag types and implement the `render` method.\n\n### `StandaloneTag`\n\n`StandaloneTag` is a tag that doesn't require a closing tag. It can be used like this:\n\n```python\nfrom datetime import datetime\nfrom jinja2_simple_tags import StandaloneTag\n\n\nclass NowExtension(StandaloneTag):\n tags = {\"now\"}\n\n def render(self, format=\"%Y-%m-%d %H:%I:%S\"):\n return datetime.now().strftime(format)\n```\n\n```jinja2\n{% now %} {# 2023-04-27 20:08:03 #}\n{% now '%m/%d/%Y' %} {# 04/27/2023 #}\n```\n\n#### Escaping\n\nBy default, the output of `StandaloneTag` will be escaped. To disable escaping,\nset the `safe_output` property of your tag to `True`:\n\n```python\nfrom jinja2_simple_tags import StandaloneTag\n\n\nclass AlertExtension(StandaloneTag):\n safe_output = True\n tags = {\"alert\"}\n\n def render(self, message):\n return \"<script>alert('{}')</script>\".format(message)\n```\n\nYou can also return a `jinja2.Markup` object from the `render()` method to explicitly \nmark the output as safe.\n\n### `ContainerTag`\n\n`ContainerTag` is a tag that requires a closing tag and can contain arbitrary content.\nIt can be used like this:\n\n```python\nimport hmac\nfrom jinja2_simple_tags import ContainerTag\n\n\nclass HMACExtension(ContainerTag):\n tags = {\"hmac\"}\n\n def render(self, secret, digest=\"sha256\", caller=None):\n content = str(caller()).encode()\n\n if isinstance(secret, str):\n secret = secret.encode()\n\n signing = hmac.new(secret, content, digestmod=digest)\n return signing.hexdigest()\n```\n\n```jinja2\n{% hmac 'SECRET', digest='sha1' %}Hello world!{% endhmac %}\n\n{# e29371e24dc99c5641681728855a92e26829e288 #}\n```\n\n### `InclusionTag`\n\n`InclusionTag` is a tag that can be used for including other templates. \nIt allows you to specify a template name or implement the `get_template_names()` \nmethod for dynamic template selection. Here's an example:\n\n```python\nfrom jinja2_simple_tags import InclusionTag\n\nclass IncludeHeader(InclusionTag):\n tags = {\"include_header\"}\n template_name = \"header.html\"\n\n def get_context(self, logo):\n return {\n \"logo\": logo\n }\n```\n\n```jinja2\n{% include_header logo=\"/static/logo.png\" %}\n```\n\n#### Context Inheritance\n\n`InclusionTag` inherits the current context from the parent template, which allows you \nto access and use variables from the parent context within the included template. \nAny additional context variables returned by the `get_context()` method are merged with \nthe inherited context.\n\n### Context\n\nCurrent context can be accessed using `self.context` attribute of the tag class:\n\n```python\nfrom jinja2_simple_tags import StandaloneTag\n\n\nclass UserNameExtension(StandaloneTag):\n tags = {\"username\"}\n\n def render(self):\n return self.context[\"user\"].username\n```\n\n### Assignment\n\nIn addition to returning the rendered value, `ContainerTag`, `StandaloneTag` and \n`InclusionTag` also supports assigning the output to a variable in the context. \nThis can be done using the `as` keyword:\n\n```jinja2\n{% now '%m/%d/%Y' as today %} \n...\n{{ today }} {# 04/27/2023 #}\n```\n\n```jinja2\n{% hmac 'SECRET', digest='sha1' as signature %}Hello world!{% endhmac %}\n...\n{{ signature }} {# e29371e24dc99c5641681728855a92e26829e288 #}\n```\n",
"bugtrack_url": null,
"license": "BSD license",
"summary": "Base classes for quick-and-easy template tag development",
"version": "0.6.1",
"project_urls": {
"Homepage": "https://github.com/dldevinc/jinja2-simple-tags"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "78058fd074379bdfeb5ce1bccf4a8e23e004e1f238938724f743267759da94e3",
"md5": "4041e94ae9cdb6ccf70c3443da631e01",
"sha256": "7b7cfa92f6813a1e0f0b61b9efcab60e6793674753e1f784ff270542e80ae20f"
},
"downloads": -1,
"filename": "jinja2_simple_tags-0.6.1-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "4041e94ae9cdb6ccf70c3443da631e01",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.6",
"size": 5817,
"upload_time": "2024-03-06T11:52:34",
"upload_time_iso_8601": "2024-03-06T11:52:34.575596Z",
"url": "https://files.pythonhosted.org/packages/78/05/8fd074379bdfeb5ce1bccf4a8e23e004e1f238938724f743267759da94e3/jinja2_simple_tags-0.6.1-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d8b7a2c8a0c60e6f2cb0dbc5020b43df7b0dc9a8fae0767c2f72665a1c7b76c9",
"md5": "762e9bb3384669594107f784b40a5c1a",
"sha256": "54abf83883dcd13f8fd2ea2c42feeea8418df3640907bd5251dec5e25a6af0e3"
},
"downloads": -1,
"filename": "jinja2-simple-tags-0.6.1.tar.gz",
"has_sig": false,
"md5_digest": "762e9bb3384669594107f784b40a5c1a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 9849,
"upload_time": "2024-03-06T11:52:36",
"upload_time_iso_8601": "2024-03-06T11:52:36.280652Z",
"url": "https://files.pythonhosted.org/packages/d8/b7/a2c8a0c60e6f2cb0dbc5020b43df7b0dc9a8fae0767c2f72665a1c7b76c9/jinja2-simple-tags-0.6.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-06 11:52:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dldevinc",
"github_project": "jinja2-simple-tags",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "pytest",
"specs": [
[
"==",
"7.0.1"
]
]
},
{
"name": "pytest",
"specs": [
[
"==",
"7.2.0"
]
]
},
{
"name": "pytest-cov",
"specs": [
[
"==",
"4.0.0"
]
]
},
{
"name": "pytest-xdist",
"specs": [
[
"==",
"3.0.2"
]
]
}
],
"tox": true,
"lcname": "jinja2-simple-tags"
}