#33819 closed New feature (wontfix)
Add shortcut for rendering multiple templates.
Reported by: | Thomas De Bonnet | Owned by: | Thomas De Bonnet |
---|---|---|---|
Component: | Template system | Version: | 4.0 |
Severity: | Normal | Keywords: | HTMX, render, template |
Cc: | Adam Johnson | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
After extensive use of HTMX and Django I found the need to have a multiple render template system. This is because i'm creating more and more partials (small template with a specific purpose).
What is good about this new feature is that you can string together multiple templates, provide one context, and render it as one template. This is particularly handy when using HTMX, because you can string together multiple small templates, and HTMX will pickup on them and replace the corresponding HTML (this is done with hx-swap-oob)
This makes the backend very flexible and customizable.
The code would live in django.shortcuts and would look like this:
def render_multiple(request, template_names, context): rendered_html = "" for template_name in template_names: rendered_html += render_to_string( template_name, context=context, request=request ) return HttpResponse(rendered_html)
It basically loops over render_to_string, and returns a rendered template.
In the view it could be used like this:
return render( request, ["foo.html", "bar.html"], # note the list context, )
Change History (3)
comment:1 by , 2 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 2 years ago
Cc: | added |
---|---|
Resolution: | → wontfix |
Status: | assigned → closed |
Summary: | Add multiple template render support → Add shortcut for rendering multiple templates. |
comment:3 by , 2 years ago
I already rejected the feature idea from django-htmx: https://github.com/adamchainz/django-htmx/pull/240 . I said it's "really a feature request for django's template engine", but mostly because I didn't see the motivation. I still don't.
Why not just make a template that uses multiple {% include %}
tags? IMO this keeps the separation between view and template aligned with Django’s M-V-T model.
Thanks for this ticket, however the current thread is to keep Django a core framework, not providing every utility which might be useful. Maybe the django-htmx package would be a good fit for this helper 🤔.
Please follow triaging guidelines with regards to wontfix tickets and take this to DevelopersMailingList, if you don't agree.