Fixed #7318 -- Cleaned up the template inheritance logic, specifically to handle the case where the parent template has no template tags/blocks. Took the opportunity to optimize the logic a little. Thanks to Matthias Kestenholz <mk@spinlock.ch> for the original report and test case.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7688 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
dc7f21daf7
commit
4317ba5799
|
@ -69,10 +69,6 @@ class ExtendsNode(Node):
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
compiled_parent = self.get_parent(context)
|
compiled_parent = self.get_parent(context)
|
||||||
pos = 0
|
|
||||||
while isinstance(compiled_parent.nodelist[pos], TextNode):
|
|
||||||
pos += 1
|
|
||||||
parent_is_child = isinstance(compiled_parent.nodelist[pos], ExtendsNode)
|
|
||||||
parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
|
parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
|
||||||
for block_node in self.nodelist.get_nodes_by_type(BlockNode):
|
for block_node in self.nodelist.get_nodes_by_type(BlockNode):
|
||||||
# Check for a BlockNode with this node's name, and replace it if found.
|
# Check for a BlockNode with this node's name, and replace it if found.
|
||||||
|
@ -83,8 +79,16 @@ class ExtendsNode(Node):
|
||||||
# parent block might be defined in the parent's *parent*, so we
|
# parent block might be defined in the parent's *parent*, so we
|
||||||
# add this BlockNode to the parent's ExtendsNode nodelist, so
|
# add this BlockNode to the parent's ExtendsNode nodelist, so
|
||||||
# it'll be checked when the parent node's render() is called.
|
# it'll be checked when the parent node's render() is called.
|
||||||
if parent_is_child:
|
|
||||||
compiled_parent.nodelist[pos].nodelist.append(block_node)
|
# Find out if the parent template has a parent itself
|
||||||
|
for node in compiled_parent.nodelist:
|
||||||
|
if not isinstance(node, TextNode):
|
||||||
|
# If the first non-text node is an extends, handle it.
|
||||||
|
if isinstance(node, ExtendsNode):
|
||||||
|
node.nodelist.append(block_node)
|
||||||
|
# Extends must be the first non-text node, so once you find
|
||||||
|
# the first non-text node you can stop looking.
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
# Keep any existing parents and add a new one. Used by BlockNode.
|
# Keep any existing parents and add a new one. Used by BlockNode.
|
||||||
parent_block.parent = block_node.parent
|
parent_block.parent = block_node.parent
|
||||||
|
|
|
@ -704,6 +704,12 @@ class Templates(unittest.TestCase):
|
||||||
# Inheritance from local context with variable parent template
|
# Inheritance from local context with variable parent template
|
||||||
'inheritance25': ("{% extends context_template.1 %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {'context_template': [template.Template("Wrong"), template.Template("1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}")]}, '1234'),
|
'inheritance25': ("{% extends context_template.1 %}{% block first %}2{% endblock %}{% block second %}4{% endblock %}", {'context_template': [template.Template("Wrong"), template.Template("1{% block first %}_{% endblock %}3{% block second %}_{% endblock %}")]}, '1234'),
|
||||||
|
|
||||||
|
# Set up a base template to extend
|
||||||
|
'inheritance26': ("no tags", {}, 'no tags'),
|
||||||
|
|
||||||
|
# Inheritance from a template that doesn't have any blocks
|
||||||
|
'inheritance27': ("{% extends 'inheritance26' %}", {}, 'no tags'),
|
||||||
|
|
||||||
### I18N ##################################################################
|
### I18N ##################################################################
|
||||||
|
|
||||||
# {% spaceless %} tag
|
# {% spaceless %} tag
|
||||||
|
|
Loading…
Reference in New Issue