def handle_extendsnode(extendsnode, context):
 if (BLOCK_CONTEXT_KEY not in context.render_context):
  context.render_context[BLOCK_CONTEXT_KEY] = BlockContext()
 block_context = context.render_context[BLOCK_CONTEXT_KEY]
 blocks = dict(((n.name, n) for n in extendsnode.nodelist.get_nodes_by_type(BlockNode)))
 block_context.add_blocks(blocks)
 compiled_parent = extendsnode.get_parent(context)
 parent_nodelist = compiled_parent.nodelist
 for node in parent_nodelist:
  if (not isinstance(node, TextNode)):
   if isinstance(node, ExtendsNode):
    return handle_extendsnode(node, context)
   break
 blocks = dict(((n.name, n) for n in parent_nodelist.get_nodes_by_type(BlockNode)))
 block_context.add_blocks(blocks)
 block_stack = []
 new_nodelist = remove_block_nodes(parent_nodelist, block_stack, block_context)
 return new_nodelist

def handle_extendsnode ( extendsnode , context ) : if ( BLOCK_CONTEXT_KEY not in context . render_context ) : context . render_context [ BLOCK_CONTEXT_KEY ] = BlockContext ( ) block_context = context . render_context [ BLOCK_CONTEXT_KEY ] blocks = dict ( ( ( n . name , n ) for n in extendsnode . nodelist . get_nodes_by_type ( BlockNode ) ) ) block_context . add_blocks ( blocks ) compiled_parent = extendsnode . get_parent ( context ) parent_nodelist = compiled_parent . nodelist for node in parent_nodelist : if ( not isinstance ( node , TextNode ) ) : if isinstance ( node , ExtendsNode ) : return handle_extendsnode ( node , context ) break blocks = dict ( ( ( n . name , n ) for n in parent_nodelist . get_nodes_by_type ( BlockNode ) ) ) block_context . add_blocks ( blocks ) block_stack = [ ] new_nodelist = remove_block_nodes ( parent_nodelist , block_stack , block_context ) return new_nodelist
create a copy of node tree of a derived template replacing all blocks tags with the nodes of appropriate blocks .