Changeset 715
- Timestamp:
- 09/28/05 23:22:09 (3 years ago)
- Files:
-
- django/trunk/django/core/template_loader.py (modified) (3 diffs)
- django/trunk/tests/othertests/templates.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/template_loader.py
r664 r715 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): … … 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 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 if self.parent: 68 self.parent.add_parent(nodelist) 69 else: 70 self.parent = BlockNode(self.name, nodelist) 70 71 71 72 class ExtendsNode(template.Node): … … 105 106 compiled_parent.nodelist[0].nodelist.append(block_node) 106 107 else: 107 # Save the original nodelist. It's used by BlockNode. 108 parent_block.original_node_list = parent_block.nodelist 108 # Keep any existing parents and add a new one. Used by BlockNode. 109 parent_block.parent = block_node.parent 110 parent_block.add_parent(parent_block.nodelist) 109 111 parent_block.nodelist = block_node.nodelist 110 112 return compiled_parent.render(context) django/trunk/tests/othertests/templates.py
r634 r715 185 185 # {% load %} tag (within a child template) 186 186 'inheritance19': ("{% extends 'inheritance01' %}{% block first %}{% load testtags %}{% echo 400 %}5678{% endblock %}", {}, '140056783_'), 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_'), 187 199 188 200 ### EXCEPTIONS ############################################################
