Code

Opened 7 years ago

Closed 5 years ago

Last modified 5 years ago

#4529 closed (wontfix)

Allow multiple blocks with the same name in one template

Reported by: Noam Raphael <spam.noam@…> Owned by: nobody
Component: Template system Version: master
Severity: 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: UI/UX:

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@…> 7 years ago.

Download all attachments as: .zip

Change History (21)

Changed 7 years ago by Noam Raphael <spam.noam@…>

comment:1 follow-up: Changed 7 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design 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)

comment:2 in reply to: ↑ 1 Changed 7 years ago by spam.noam@…

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 7 years ago by SmileyChris

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 7 years ago by 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 %}

comment:5 Changed 7 years ago by ubernostrum

  • Resolution set to wontfix
  • Status changed from new to closed

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

comment:6 Changed 6 years ago by adrian.dziubek@…

  • Resolution wontfix deleted
  • Status changed from closed to 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 6 years ago by pixelcort

  • 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 6 years ago by ubernostrum

  • Resolution set to wontfix
  • Status changed from reopened to 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 6 years ago by noamr

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 6 years ago by SmileyChris

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 Changed 6 years ago by edrex

opened #9248

comment:12 Changed 6 years ago by edrex

SmileyChris: let's have it then!

comment:13 Changed 6 years ago by edrex

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

comment:14 Changed 6 years ago by SmileyChris

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

comment:15 Changed 6 years ago by SmileyChris

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

comment:16 Changed 5 years ago by chrisdrackett

  • Cc drackett@… added

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

comment:17 Changed 5 years ago by anonymous

  • Resolution wontfix deleted
  • Status changed from closed to reopened

comment:18 Changed 5 years ago by Alex

  • Resolution set to wontfix
  • Status changed from reopened to 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 5 years ago by emh

  • 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 5 years ago by SmileyChris

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.