Opened 8 weeks ago

Last modified 5 weeks ago

#35790 assigned Cleanup/optimization

Deprecate content outside of {% block %} tags when {% extends %} is used

Reported by: Adam Johnson Owned by: Sanjeev Holla S
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Sanjeev Holla S Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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'

Change History (4)

comment:1 by Sarah Boyce, 8 weeks ago

Triage Stage: UnreviewedAccepted

comment:2 by Sanjeev Holla S, 8 weeks ago

Cc: Sanjeev Holla S added
Owner: set to Sanjeev Holla S
Status: newassigned

comment:3 by Sanjeev Holla S, 8 weeks ago

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)

comment:4 by Carlton Gibson, 5 weeks 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.

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