Unnecessary usage of NodeList in ForNode (template rendering)
|Reported by:||anonymous||Owned by:||nobody|
|Cc:||Dmitry Trofimov, FunkyBob||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
There is some redundant rendering of nodes happening in template for loops:
ForNode the rendering is done as follows:
- A new
- The nodes inside the for loop are rendered in the for loop's context and pushed to the nodelist
- The nodelist is rendered in the context containing the for loop
The code is at http://code.djangoproject.com/browser/django/trunk/django/template/defaulttags.py, lines 146, 178 and 187.
Now, the last step seems redundant and wrong. It is redundant because every node was rendered in step 2 above, so why is it needed to render the nodelist again? If there is some reason for this, there needs to be a comment explaining why. If there is no reason for this second rendering, it would be more effective to change the nodelist to a plain list and return
u''.join(the_list) from the
ForNode.render(). The last step is wrong, because in step 2 if a node returns something that can be rendered, then that thing will be rendered in context outside of the for loop's in step 3.