Skip to main content

Render the content of a specific block tag from a Jinja2 template.

Project description

django-jinja-render-block

pypi

This project aims to bring the super ergomomic partial template rendering of django-template-partials to django-jinja. It's a bit more limited in scope though: instead of allowing you to define reusable (inline) template partials using {% partialdef partial-name %}, it limits itself to rendering a block within a template, ala django-render-block.

TL;DR: you can render just a block using a template name such as template-name.jinja#block-name.

Installation

uv add django-jinja-render-block

Then use its template backend, instead of the one from django-ninja:

TEMPLATES = [
    {
        "BACKEND": "render_block.backend.Jinja2", # <- instead of "django_jinja.jinja2.Jinja2"
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            # ...
        },
    },
]

Usage

Define a block in your Jinja template:

{% extends "base.jinja" %}

{% block body %}
<h1>HOME</h1>

{% block test-partial %}
<p>Partial block content</p>
{% endblock %}

<p>Lorum ipsum</p>
{% endblock %}

And in your view code you use the template name plus the partial block name, like so:

# In view handler...
self.template_name = "example.jinja#test-partial"

This is extremely useful in combination with HTMX:

def partial_rendering(request: HtmxHttpRequest) -> HttpResponse:
    template_name = "example.jinja"
    if request.htmx:
        template_name += "#test-partial"

    return TemplateResponse(request, template_name)

Thanks to

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

django_jinja_render_block-0.2.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_jinja_render_block-0.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file django_jinja_render_block-0.2.0.tar.gz.

File metadata

File hashes

Hashes for django_jinja_render_block-0.2.0.tar.gz
Algorithm Hash digest
SHA256 95f0eb2c9224062c223cb2d2a485646345de1198e73251b36fb61741100259c2
MD5 c81d5ee73e6280d8d0ad64e6eda134d4
BLAKE2b-256 68c5d5676f6a41191ef1906eba08a5cc03ec0dfbb43aa9c065112cf1ffe59810

See more details on using hashes here.

File details

Details for the file django_jinja_render_block-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_jinja_render_block-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4302e87eb04ffa41f2965736c1b784ba6b85ffbf4894eb9021d5296d337c111
MD5 acd5bd0e0cfcee753f4eb0c3778e4a54
BLAKE2b-256 4558283fa500c8493e75c70c02c7eeccd5c72d68f44e59a63442f8a5a9065c41

See more details on using hashes here.

Supported by

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