Opened 11 years ago

Closed 8 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


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?

Change History (7)

comment:1 Changed 11 years ago by miracle2k

comment:2 Changed 11 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 11 years ago by Adrian Holovaty

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

comment:4 Changed 11 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 11 years ago by Eric Holscher

Needs tests: set
Triage Stage: UnreviewedDesign decision needed

comment:6 Changed 8 years ago by Luke Plant

Severity: Normal
Type: Bug

comment:7 Changed 8 years ago by Carl Meyer

Easy pickings: unset
Resolution: needsinfo
Status: newclosed
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.

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