Opened 3 hours ago
Last modified 3 hours ago
#36130 new New feature
Add inclusion support for simple block tags
Reported by: | Jake Howard | Owned by: | |
---|---|---|---|
Component: | Template system | Version: | |
Severity: | Normal | Keywords: | |
Cc: | Jake Howard | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Django's template system allows creating tags which include other templates:
@register.inclusion_tag("results.html") def show_results(poll): choices = poll.choice_set.all() return {"choices": choices}
However, with the addition of simple_block_tag
in #35535, it would be nice to combine the 2 into a inclusion_block_tag
. This creates a form of reusable component for Django template, combining context transformation, rendering and internal content. There are a number of libraries in the Django ecosystem which achieve this (eg slippers
), so there's clearly an appetite for this functionality.
@register.inclusion_block_tag("results.html") def show_results(content, poll): choices = poll.choice_set.all() return {"content": content, "choices": choices}
{% show_results poll %} This content gets captured too {% endmysimpletag %}
The API matches the existing inclusion_tag
, but with an additional required content
argument containing the (rendered) template content.
An open question I have about this approach: Should
content
be implicitly passed through to the template, or require passing in via the context? It feels like a case of "explicit > implicit", however it's also a fairly easy foot-gun and confusion for developers.