Render the content of a specific block tag from a Jinja2 template.
Project description
django-jinja-render-block
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
- https://github.com/carltongibson/django-template-partials for the initial idea. I love the way it allows you to render a partial template just by appending the partial name to the template name. It only works with the Django Template Language though.
- https://github.com/clokep/django-render-block for showing me how to render a single block out of a template.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_jinja_render_block-0.1.0.tar.gz.
File metadata
- Download URL: django_jinja_render_block-0.1.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
475ffde88db233a2c8821838b67ee860ae2d65d879a53166288d01aa9afaafad
|
|
| MD5 |
42460dccc30f60addad82baa366ba730
|
|
| BLAKE2b-256 |
5b54164e5df167c8229ef9023ddc08bacdf76ac01271da9e289f0fd64983d25c
|
File details
Details for the file django_jinja_render_block-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_jinja_render_block-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
912d435f368fc5be200029c044cc73e906aeba238f6aa8fc653efef57cfcffeb
|
|
| MD5 |
1a8a46c67482dcd9f1dc013cc6079a0e
|
|
| BLAKE2b-256 |
54809000c8bd659976f8db3c58cd724fc4c9e2666d92a520ae440a685469a0c2
|