Opened 6 years ago

Closed 6 years ago

Last modified 5 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:

Description

Here is the http://dpaste.com/40134/ 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 6 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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

  • Resolution invalid deleted
  • Status changed from closed to reopened

you can paste this at the end of your settings.py

from django.template.defaulttags import IfEqualNode

def get_nodes_by_type(self, nodetype):
   nodes = []
   if isinstance(self, nodetype):
      nodes.append(self)
   nodes.extend(self.nodelist_true.get_nodes_by_type(nodetype))
   nodes.extend(self.nodelist_false.get_nodes_by_type(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 6 years ago by ubernostrum

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

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

Fixed in [12655], by the way.

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