Ticket #501: block_super_fix.diff
File block_super_fix.diff, 3.8 KB (added by , 19 years ago) |
---|
-
django/core/template_loader.py
42 42 # If we get here, none of the templates could be loaded 43 43 raise template.TemplateDoesNotExist, ', '.join(template_name_list) 44 44 45 class SuperBlock:46 "This implements the ability for {{ block.super }} to render the parent block's contents"47 def __init__(self, context, nodelist):48 self.context, self.nodelist = context, nodelist49 50 def super(self):51 if self.nodelist:52 return self.nodelist.render(self.context)53 else:54 return ''55 56 45 class BlockNode(template.Node): 57 def __init__(self, name, nodelist ):58 self.name, self.nodelist = name, nodelist46 def __init__(self, name, nodelist, parent=None): 47 self.name, self.nodelist, self.parent = name, nodelist, parent 59 48 60 49 def __repr__(self): 61 50 return "<Block Node: %s. Contents: %r>" % (self.name, self.nodelist) 62 51 63 52 def render(self, context): 64 53 context.push() 65 nodelist = hasattr(self, 'original_node_list') and self.original_node_list or None 66 context['block'] = SuperBlock(context, nodelist) 54 # Save context in case of block.super() 55 self.context = context 56 context['block'] = self 67 57 result = self.nodelist.render(context) 68 58 context.pop() 69 59 return result 70 60 61 def super(self): 62 if self.parent: 63 return self.parent.render(self.context) 64 return '' 65 66 def add_parent(self, nodelist): 67 # Recurse to add parent to eldest position 68 if self.parent: 69 self.parent.add_parent(nodelist) 70 else: 71 self.parent = BlockNode(self.name, nodelist) 72 71 73 class ExtendsNode(template.Node): 72 74 def __init__(self, nodelist, parent_name, parent_name_var, template_dirs=None): 73 75 self.nodelist = nodelist … … 104 106 if parent_is_child: 105 107 compiled_parent.nodelist[0].nodelist.append(block_node) 106 108 else: 107 # Save the original nodelist. It's used by BlockNode. 108 parent_block.original_node_list = parent_block.nodelist 109 # Keep any existing parents and add a new one. Used by BlockNode. 110 parent_block.parent = block_node.parent 111 parent_block.add_parent(parent_block.nodelist) 109 112 parent_block.nodelist = block_node.nodelist 110 113 return compiled_parent.render(context) 111 114 -
tests/othertests/templates.py
185 185 # {% load %} tag (within a child template) 186 186 'inheritance19': ("{% extends 'inheritance01' %}{% block first %}{% load testtags %}{% echo 400 %}5678{% endblock %}", {}, '140056783_'), 187 187 188 # Two-level inheritance with {{ block.super }} 189 'inheritance20': ("{% extends 'inheritance01' %}{% block first %}{{ block.super }}a{% endblock %}", {}, '1_a3_'), 190 191 # Three-level inheritance with {{ block.super }} from parent 192 'inheritance21': ("{% extends 'inheritance02' %}{% block first %}{{ block.super }}a{% endblock %}", {}, '12a34'), 193 194 # Three-level inheritance with {{ block.super }} from grandparent 195 'inheritance22': ("{% extends 'inheritance04' %}{% block first %}{{ block.super }}a{% endblock %}", {}, '1_a3_'), 196 197 # Three-level inheritance with {{ block.super }} from parent and grandparent 198 'inheritance23': ("{% extends 'inheritance20' %}{% block first %}{{ block.super }}b{% endblock %}", {}, '1_ab3_'), 199 188 200 ### EXCEPTIONS ############################################################ 189 201 190 202 # Raise exception for invalid template name