Opened 17 years ago

Closed 16 years ago

Last modified 10 years ago

#4529 closed Uncategorized (wontfix)

Allow multiple blocks with the same name in one template

Reported by: Noam Raphael <spam.noam@…> 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:

  1. 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.
  1. 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)

allowmultipleblocks.diff (7.8 KB ) - added by Noam Raphael <spam.noam@…> 17 years ago.

Download all attachments as: .zip

Change History (22)

by Noam Raphael <spam.noam@…>, 17 years ago

Attachment: allowmultipleblocks.diff added

comment:1 by Chris Beaven, 17 years ago

Triage Stage: UnreviewedDesign decision needed

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)

in reply to:  1 comment:2 by spam.noam@…, 17 years ago

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 by Chris Beaven, 17 years ago

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 by Batiste Bieler, 17 years ago

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 by James Bennett, 17 years ago

Resolution: wontfix
Status: newclosed

Marking wontfix since it's gone several months with no real activity.

comment:6 by adrian.dziubek@…, 16 years ago

Resolution: wontfix
Status: closedreopened

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 by pixelcort, 16 years ago

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 by James Bennett, 16 years ago

Resolution: wontfix
Status: reopenedclosed

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 by noamr, 16 years ago

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 by Chris Beaven, 16 years ago

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:11 by edrex, 16 years ago

opened #9248

comment:12 by edrex, 16 years ago

SmileyChris: let's have it then!

comment:13 by edrex, 16 years ago

ubernostrum: why does lack of activity justify wontfix? can we reopen this?

comment:14 by Chris Beaven, 16 years ago

Ok ok, I'll try to dig up the code tomorrow :P

comment:15 by Chris Beaven, 16 years ago

See #9256 for the core change that I need to get the tag working.

comment:16 by chrisdrackett, 16 years ago

Cc: drackett@… added

whatever happened to this ticket? is this possible now? (I might be missing something)

comment:17 by anonymous, 16 years ago

Resolution: wontfix
Status: closedreopened

comment:18 by Alex Gaynor, 16 years ago

Resolution: wontfix
Status: reopenedclosed

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 by emh, 15 years ago

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 by Chris Beaven, 15 years ago

Here's an example {% repeatblock %} tag btw: http://github.com/SmileyChris/django-repeatblock

comment:21 by Chris Kief, 10 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

Another application for repeating blocks:
https://github.com/nalourie/django-macros

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