Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#10975 closed (wontfix)

Accessing derived block doesn't work within ifequal tag

Reported by: libwilliam@… Owned by: nobody
Component: Uncategorized Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Here is the code of what I mean. Essentially using a block in a derived template that was specified from within an ifequal tag in the parent template doesn't work.

Change History (6)

comment:1 Changed 7 years ago by James Bennett

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: invalid
Status: newclosed

A block cannot be defined conditionally within any sort of "if" construct, and this is by design; the structure of the template is the structure of the template.

comment:2 Changed 7 years ago by findepi

IMHO, this is not true: a block *can* be defined in a if construct, but not in ifequal, ifnotequal.

comment:3 Changed 7 years ago by findepi

Moreover, if it's design decision (IMO, an arguable one:), then there should be a template syntax error or something. Instead everything works almost fine, but the block cannot be redefined. Users may spend lots of time before searching the tickets database just to read "hey, it's meant to be so".

Why this design is such and not different? I see no clean solution for my design. I have a generic template with few if/ifequal statements, and the other template extending the first one and redefining some blocks. I know the redefining makes sense, I know that the original blocks would be displayed (and yes, they are displayed). If am not allowed to redefine them (e.g. redefine labels, snippets, small widgets) I'm forced to pass them in the rendering context, thus the context will have to contain some HTML snippets -- which I personally treat a poor design.

I this ticket is to remain closed, can you, please, tell how I (and other users:) should construct their templates?

comment:4 Changed 7 years ago by Damien Nozay

Resolution: invalid
Status: closedreopened

you can paste this at the end of your

from django.template.defaulttags import IfEqualNode

def get_nodes_by_type(self, nodetype):
   nodes = []
   if isinstance(self, nodetype):
   return nodes

IfEqualNode.get_nodes_by_type = get_nodes_by_type

IMHO, this should be reopened and fixed.
Users may want to display things differently according to a condition,
e.g. I would want to have that in a base template:

{% ifnotequal setting u'inverted'%}
   {% block a %}{% endblock %}
   {% block b %}{% endblock %}
{% else %}
   {% block b %}{% endblock %}
   {% block a %}{% endblock %}
{% endifnotequal %}

comment:5 Changed 7 years ago by James Bennett

Resolution: wontfix
Status: reopenedclosed

Repeating myself:

"A block cannot be defined conditionally within any sort of "if" construct, and this is by design; the structure of the template is the structure of the template."

If you disagree, please take it to the django-developers list.

comment:6 Changed 7 years ago by Chris Beaven

Fixed in [12655], by the way.

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