Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#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 Thomas De Bonnet, 2 years ago

Owner: changed from nobody to Thomas De Bonnet
Status: newassigned

comment:2 by Mariusz Felisiak, 2 years ago

Cc: Adam Johnson added
Resolution: wontfix
Status: assignedclosed
Summary: Add multiple template render supportAdd shortcut for rendering multiple templates.

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.

comment:3 by Adam Johnson, 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.

Note: See TracTickets for help on using tickets.
Back to Top