No project description provided
Project description
Jinja2 Components
Template components for Jinja2, inspiration from django-components
and jinja2-simple-tags
.
Usage
Standalone tag replacement with specified template
from jinja2 import Environment, Template
from jinja2_components import ComponentsExtension, Component, register
env = Environment(extensions=[ComponentsExtension])
@register(name="hello")
class Hello(Component):
template = Template("hello")
template = env.from_string("{% hello %} world")
print(template.render())
# hello world
Processing body of tag
from base64 import b64encode
from jinja2 import Environment, Template
from jinja2_components import ComponentsExtension, Component, register
env = Environment(extensions=[ComponentsExtension])
@register(name="base64")
class Base64(Component):
template = Template("{{ result }}")
block = True
@classmethod
def get_context(cls, caller):
content = str(caller()).encode()
return {"result": b64encode(content).decode()}
template = env.from_string("{% base64 %}hello world{% endbase64 %}")
print(template.render())
# aGVsbG8gd29ybGQ=
template = env.from_string("""\
{% base64 as hw_base64 %}hello world{% endbase64 %}\
Base64 of 'hello world': {{ hw_base64 }}
""")
print(template.render())
# Base64 of 'hello world': aGVsbG8gd29ybGQ=
Project details
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_components-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96361b893fe4b9989985948c7a55c00acab5ec70d786ff318d07205ae6c4530d |
|
MD5 | 8943bc56c2122d8171ca60a230e819f0 |
|
BLAKE2b-256 | 454cc41f305b4dfde285b297026ebd61839eb2c24fe92fda2ca5bbacdf1bde6c |