#4529 closed Uncategorized (wontfix)
Allow multiple blocks with the same name in one template
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | me@…, drackett@…, emh | Triage Stage: | Design decision needed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This patch allows multiple blocks with the same name in one template as long as they have exactly the same content. It turns out that this restriction is sufficient in order to keep the block system consistent.
A simple example:
base.html:
<html> <head> <title>Bug System - {% block title %}Default Title{% endblock %}</title> </head> <body> <h1>{% block title %}Default Title{% endblock %}</h1> {% block body %}Default Body{% endblock %} </body> </html>
The template:
{% extends 'base.html' %} {% block title %}Title!{% endblock %} {% block body %}Body!{% endblock %}
The result:
<html> <head> <title>Bug System - Title!</title> </head> <body> <h1>Title!</h1> Body! </body> </html>
See the discussion at http://groups.google.com/group/django-developers/t/d100212a99e2a653
Two notes about the patch:
- I moved "add_to_builtins('django.template.loader_tags')" from template/loader.py to
template/__init__.py
, because loader_tags wasn't loaded for me even for the SVN without my changes.
- The change to
__init__.py
seems big, but it's mostly indentation, since I put most of the code in a try...finally block.
Attachments (1)
Change History (22)
Changed 16 years ago by
Attachment: | allowmultipleblocks.diff added |
---|
comment:1 follow-up: 2 Changed 16 years ago by
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:2 Changed 16 years ago by
Well, if I think about it, Malcolm's idea is nice.
The exception raised when someone uses two blocks with the same name can mention reuse-block.
Should I create a patch to implement it?
Noam
comment:3 Changed 16 years ago by
Noam - open a new ticket about that and if you don't want to put in too much effort, ask about it in the django-dev group before starting the patch.
If you're happy to drop this ticket, then feel free to close it as wontfix (that's probably where it's heading anyway)
comment:4 Changed 16 years ago by
A workaround if you need to block that have the same content in your base.html is to nest your block in your child template like this:
{% block title %}{% block page_title %}{% trans "My page title" %}{% endblock %}{% endblock %}
comment:5 Changed 16 years ago by
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Marking wontfix since it's gone several months with no real activity.
comment:6 Changed 15 years ago by
Resolution: | wontfix |
---|---|
Status: | closed → reopened |
I stumbled upon this, while trying to make a first page. The explanation in docs says:
Finally, note that you can’t define multiple {% block %} tags with the same name in the same template. This limitation exists because a block tag works in “both” directions. That is, a block tag doesn’t just provide a hole to fill — it also defines the content that fills the hole in the parent. If there were two similarly-named {% block %} tags in a template, that template’s parent wouldn’t know which one of the blocks’ content to use.
If that's the reasoning behind, then the error should only appear, when the template is used as a leaf of inheritance. And even then it would be much more intuitive, to just use the defined content. The user would get that something is wrong, if he got the content twice. Anyway the patch would also do the work for me. And there are at least two threads found on google for that problem.
comment:7 Changed 15 years ago by
Cc: | me@… added |
---|
Replying to Batiste Bieler:
A workaround if you need to block that have the same content in your base.html is to nest your block in your child template like this:
{% block title %}{% block page_title %}{% trans "My page title" %}{% endblock %}{% endblock %}
This workaround works, but is inconvenient. I have the same situation where I want to set my title and h1 to the same thing in my base.html.
comment:8 Changed 15 years ago by
Resolution: | → wontfix |
---|---|
Status: | reopened → closed |
This is still pretty much by design based on how the template system works. And if you have some bit of content you want to reuse in multiple places in a template, well, that's what template variables are for.
comment:9 Changed 15 years ago by
I must disagree. I don't believe that the template system was designed to make me avoid using the same text as the window title and inside the page. And I'm certain that the title should be set in the template, and not in the views.py file (as that's where template variables are set).
comment:10 Changed 15 years ago by
I wrote a {% repeatblock %}
tag a while ago - it does need a small change to core, but I should dig it out and post it as a new ticket. I agree with noamr's sentiment, but disagree with the title (and implementation) of this ticket.
Malcolm suggested something similar to my tag in the django-dev thread, so at least my minor template change could stand a chance.
comment:13 Changed 15 years ago by
ubernostrum: why does lack of activity justify wontfix? can we reopen this?
comment:15 Changed 15 years ago by
See #9256 for the core change that I need to get the tag working.
comment:16 Changed 15 years ago by
Cc: | drackett@… added |
---|
whatever happened to this ticket? is this possible now? (I might be missing something)
comment:17 Changed 15 years ago by
Resolution: | wontfix |
---|---|
Status: | closed → reopened |
comment:18 Changed 15 years ago by
Resolution: | → wontfix |
---|---|
Status: | reopened → closed |
It was wonfixed by a core developer(quite a while ago), if you disagree the correct course of action is to bring it up on django-dev
comment:19 Changed 14 years ago by
Cc: | emh added |
---|
It would have saved me some time if this ticket was linked from http://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance or the workaround documented there.
comment:20 Changed 14 years ago by
Here's an example {% repeatblock %}
tag btw: http://github.com/SmileyChris/django-repeatblock
comment:21 Changed 9 years ago by
Easy pickings: | unset |
---|---|
Severity: | → Normal |
Type: | → Uncategorized |
UI/UX: | unset |
Another application for repeating blocks:
https://github.com/nalourie/django-macros
I'm pushing to design decision, but since this hasn't received very good feedback I wouldn't be surprised if it is closed as wontfix soon...
(personally, I much prefer Malcolm's idea of just creating a
{% reuse-block %}
tag)