Skip to main content

Streamlined way to build and reuse your Jinja2 templates from code

Project description

Jinja2 Components

Jinja2 Components offers a streamlined way to build and reuse your Jinja2 templates from code, inspired by django-components and jinja2-simple-tags.

Note: This library is under active development, so the API may change.

Key Ideas

  • Modular Design. Break down your templates into reusable components for cleaner code and easier maintenance.
  • Simple Syntax. Use Jinja2 syntax to only use your components, lower you templates depth.
  • Extensible Framework. Customize your components using Python code.

Usage

  1. Install the library.

    pip install jinja2-components
    
  2. Add extension to the Jinja2 environment.

    from jinja2 import Environment
    from jinja2_components import ComponentsExtension
    
    env = Environment(extensions=[ComponentsExtension])
    
  3. Define a component and it's template.

    from jinja2 import Template
    from jinja2_components import Component, register
    
    @register(name="hello")
    class Hello(Component):
        template = Template("hello")
    
  4. Use component inside template as a tag.

    template = env.from_string("{% hello %} world")
    print(template.render())
    # hello world
    

Examples

Standalone tag

@register(name="hello")
class Hello(Component):
    template = Template("hello")

template = env.from_string("{% hello %} world")
print(template.render())
# hello world

Block tag with body

@register(name="button")
class Button(Component):
    template = Template("<button>{{ body }}</button>")
    block = True

    @classmethod
    def get_context(cls, caller):
        return {"body": str(caller())}

template = env.from_string("{% button %}Hello!{% endbutton %}")
print(template.render())
# <button>Hello!</button>

Replacing body

@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=

Component in component

If the component's template was set directly, then this template also requires extensions. You can pass extensions where required or as a workaround use env.from_string.

But it possible to use template_str and template_name class variables for later instantiation from environment.

@register(name="button")
class Button(Component):
    template_str = "<button>{{ body }}</button>"

@register(name="menu")
class Menu(Component):
    template_str = """\
<div class="menu">\
{% for btn in buttons %}
  {% button body=btn %}\
{% endfor %}
</button>
"""

template = env.from_string("{% menu buttons=[1, 2, 3] %}")
print(template.render())
# <div class="menu">
#   <button>1</button>
#   <button>2</button>
#   <button>3</button>
# </button>

Rendering component from code

The idea behind this feature is to pass arguments and get rendered component inside the code.

@register(name="button")
class Button(Component):
    template_str = "<button>{{ body }}</button>"

@register(name="menu")
class Menu(Component):
    template_str = """\
<div class="menu">\
{% for btn in buttons %}
  {% button body=btn %}\
{% endfor %}
</button>
"""

template = env.from_string("{% menu buttons=[1, 2, 3] %}")
print(template.render())
# <div class="menu">
#   <button>1</button>
#   <button>2</button>
#   <button>3</button>
# </button>

print(Menu(env, buttons=[1, 2, 3]))
# <div class="menu">
#   <button>1</button>
#   <button>2</button>
#   <button>3</button>
# </button>

Also it is possible to pass initialized components (because the are strings).

@register(name="menu")
class Menu(Component):
    template_str = """\
<div class="menu">\
{% for btn in buttons %}
  {{ btn }}\
{% endfor %}
</button>
"""

rendered = Menu(env, buttons=[Button(env, body=1), Button(env, body=2), Button(env, body=3)])
print(rendered)
# <div class="menu">
#   <button>1</button>
#   <button>2</button>
#   <button>3</button>
# </button>

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

jinja2_components-0.0.4.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

jinja2_components-0.0.4-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file jinja2_components-0.0.4.tar.gz.

File metadata

  • Download URL: jinja2_components-0.0.4.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1025-azure

File hashes

Hashes for jinja2_components-0.0.4.tar.gz
Algorithm Hash digest
SHA256 c5abeca92548b9ae4b7a1056adfc1ce77ea807791d77a1fa75fd90e4a3e22a6d
MD5 af939902e5c1b01bbb9e2767b530e1d3
BLAKE2b-256 a7fa58bd3ffdf7be62970b7eb49d903684d543f0d88ded83ad8d377b88278ce7

See more details on using hashes here.

File details

Details for the file jinja2_components-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: jinja2_components-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1025-azure

File hashes

Hashes for jinja2_components-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 efc8884548f4217eb2689993f370aa212ebc0fcbf114611771280ed9f7ff9c4d
MD5 4a0b30d1c217f778c30a3b9753ed25a0
BLAKE2b-256 d4b44c260264be878699af032c0a5532f8b36796428564e37996cd5ec9de97bc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page