app/django/template/loader_tags.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
    67         else:
    67         else:
    68             return get_template_from_string(source, origin, parent)
    68             return get_template_from_string(source, origin, parent)
    69 
    69 
    70     def render(self, context):
    70     def render(self, context):
    71         compiled_parent = self.get_parent(context)
    71         compiled_parent = self.get_parent(context)
    72         pos = 0
       
    73         while isinstance(compiled_parent.nodelist[pos], TextNode):
       
    74             pos += 1
       
    75         parent_is_child = isinstance(compiled_parent.nodelist[pos], ExtendsNode)
       
    76         parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
    72         parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)])
    77         for block_node in self.nodelist.get_nodes_by_type(BlockNode):
    73         for block_node in self.nodelist.get_nodes_by_type(BlockNode):
    78             # Check for a BlockNode with this node's name, and replace it if found.
    74             # Check for a BlockNode with this node's name, and replace it if found.
    79             try:
    75             try:
    80                 parent_block = parent_blocks[block_node.name]
    76                 parent_block = parent_blocks[block_node.name]
    81             except KeyError:
    77             except KeyError:
    82                 # This BlockNode wasn't found in the parent template, but the
    78                 # This BlockNode wasn't found in the parent template, but the
    83                 # parent block might be defined in the parent's *parent*, so we
    79                 # parent block might be defined in the parent's *parent*, so we
    84                 # add this BlockNode to the parent's ExtendsNode nodelist, so
    80                 # add this BlockNode to the parent's ExtendsNode nodelist, so
    85                 # it'll be checked when the parent node's render() is called.
    81                 # it'll be checked when the parent node's render() is called.
    86                 if parent_is_child:
    82 
    87                     compiled_parent.nodelist[pos].nodelist.append(block_node)
    83                 # Find out if the parent template has a parent itself
       
    84                 for node in compiled_parent.nodelist:
       
    85                     if not isinstance(node, TextNode):
       
    86                         # If the first non-text node is an extends, handle it.
       
    87                         if isinstance(node, ExtendsNode):
       
    88                             node.nodelist.append(block_node)
       
    89                         # Extends must be the first non-text node, so once you find
       
    90                         # the first non-text node you can stop looking. 
       
    91                         break
    88             else:
    92             else:
    89                 # Keep any existing parents and add a new one. Used by BlockNode.
    93                 # Keep any existing parents and add a new one. Used by BlockNode.
    90                 parent_block.parent = block_node.parent
    94                 parent_block.parent = block_node.parent
    91                 parent_block.add_parent(parent_block.nodelist)
    95                 parent_block.add_parent(parent_block.nodelist)
    92                 parent_block.nodelist = block_node.nodelist
    96                 parent_block.nodelist = block_node.nodelist