Opened 5 months ago
Last modified 4 months ago
#35790 assigned Cleanup/optimization
Deprecate content outside of {% block %} tags when {% extends %} is used
Description ¶
Currently, content outside of {% block %}
tags in an extending template is silently discarded. This could be a source of bugs, where written content may appear to be rendered at first glance, but isn't.
I propose we deprecate allowing non-whitespace text nodes outside of {% block %}
tags, when {% extends %}
is used. I don't think we can disallow tags because they might be tags like {% load %}
or {% comment %}
or a custom tag with other side effects.
For example, take this base template egg.html
:
{% block yolk %} hi {% endblock yolk %}
And an extending template, chicken.html
:
{% extends 'egg.html' %} <title>Chicken egg</title> {% block yolk %} yellow {% endblock yolk %}
The <title>
content is silently discarded:
In [1]: from django.template import loader In [2]: loader.get_template("chicken.html") Out[2]: <django.template.backends.django.Template at 0x17431a950> In [3]: loader.get_template("chicken.html").render() Out[3]: '\nyellow\n\n'
According to the ticket's flags, the next step(s) to move this issue forward are:
- To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is:
[https://github.com/django/django/pull/#### PR]
.
Change History (4)
comment:1 by , 5 months ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 5 months ago
Cc: | added |
---|---|
Owner: | set to |
Status: | new → assigned |
comment:3 by , 5 months ago
comment:4 by , 4 months ago
I use such content quite regularly, for scratchpads and documentation. It seems a bit mean to force that inside a comment, but I guess that would be doable 🤔
… a custom tag with other side effects.
Like a partialdef tag for example.
Hello
So whenever a non-whitespace text nodes are used outside a {% block %} tag, a deprication warning needs to be added right? (only when the templates are extended from a base template)