🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

django-jinja-render-block

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

django-jinja-render-block

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

0.3.0
PyPI
Maintainers
1

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:

class ExamplePartialView(TemplateView):
    template_name = "example.jinja#test-partial"

This is extremely useful in combination with HTMX:

def example_view(request):
    template_name = "example.jinja"
    if request.htmx:
        template_name += "#test-partial"

    return TemplateResponse(request, template_name)

request.htmx comes from django-htmx

Thanks to

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts