Code

Opened 6 years ago

Closed 3 years ago

#7430 closed Bug (needsinfo)

Recursively iterating through a template's nodes

Reported by: miracle2k Owned by: nobody
Component: Template system Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Right now, for node in Template() will only yield the top-level nodes. Is this the way it is intended to work? I was expecting to get all nodes, recursively.

The Template.__iter__ code looks like this:

    def __iter__(self):
        for node in self.nodelist:
            for subnode in node:
                yield subnode

And Node.__iter__ does:

    def __iter__(self):
        yield self

This looks like a precipe to allow nodes to yield their childnodes, without relying on the existence of the nodelist attribute.

However, nodes like BlockNode and ExtendsNode do not implement __iter__ - only ForNode and IfNode seem to do, and not in the way I would have expected (they don't yield self).

Is this a bug, or per design?

Attachments (0)

Change History (7)

comment:1 Changed 6 years ago by miracle2k

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by miracle2k

I'm noticing that both ForNode and IfNode do not provide the nodelist attribute. Is the idea that you should use Node.nodelist if it exists, and fall back to the iterator otherwise? Wouldn't it make sense then to have the Node class implement a generic iterator that will yield the items in self.nodelist?

comment:3 Changed 6 years ago by adrian

I don't recall whether this was by design. Does any Django code actually *use* this iter behavior?

comment:4 Changed 6 years ago by miracle2k

Any of Django's code itself? I quick grep didn't reveal anything obvious. Rendering works via recursive render() calls, so that part doesn't need it.

comment:5 Changed 6 years ago by ericholscher

  • Needs tests set
  • Triage Stage changed from Unreviewed to Design decision needed

comment:6 Changed 3 years ago by lukeplant

  • Severity set to Normal
  • Type set to Bug

comment:7 Changed 3 years ago by carljm

  • Easy pickings unset
  • Resolution set to needsinfo
  • Status changed from new to closed
  • UI/UX unset

Code changes require more practical justification than this. Feel free to reopen with demonstration of a specific use case, the code that is currently required to achieve it, and the code that would be possible with a change to Django.

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.