I’d like to begin a discussion about adding some kind of inline partials or template fragments feature to the DTL.
For background, there’s an HTMX essay on this topic here: </> Htmx ~ Template Fragments
The basic idea is that you can re-use parts of a template, when just updating part of a page say.
Currently the DTL requires moving the reusable fragment into an include or a template tag, which is great in many cases, but can be a little heavyweight (certainly to begin). Being able to define these partials inline gives better Locality of Behaviour (a concept for which there’s another essay on the HTMX site).
There’s lots of history in the area — here’s one thread, that links to another, but there are many more similar ideas floated around — so there’s clearly some demand.
I gave a talk on this topic at DjangoCon Europe this year, and have put up a package called django-template-partials
to help demonstrate the concept and drive discussion:
I’ll write up further thoughts, but will just note two points for now:
- There’s a similar package called
django-render-block
that’s in play, but:- I wanted partials to be reusable within a single template, which blocks are not.
- I integrated with the template loader, so bar changing a template name, it’s transparent to the view layer, which means not rewriting view logic.
- The first-pass implementation required attaching a mapping to the template origin — in order to get info out of the parser. A good first step would be pondering if we can adjust the API there allow this in a cleaner way.
I think we should play with a few ideas before rushing into an implementation here for Django — no doubt there’ll be both good ideas and unforeseen problems. I think stating now that Django 5.1 at the earliest would allow the whole summer and more to experiment.
But I think this is something we should add now. So putting it out there to you
Thanks.