Ticket #501: block_super_fix.diff

File block_super_fix.diff, 3.8 KB (added by django@…, 19 years ago)

A hack for this problem and some unit tests

  • django/core/template_loader.py

     
    4242    # If we get here, none of the templates could be loaded
    4343    raise template.TemplateDoesNotExist, ', '.join(template_name_list)
    4444
    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, nodelist
    49 
    50     def super(self):
    51         if self.nodelist:
    52             return self.nodelist.render(self.context)
    53         else:
    54             return ''
    55 
    5645class BlockNode(template.Node):
    57     def __init__(self, name, nodelist):
    58         self.name, self.nodelist = name, nodelist
     46    def __init__(self, name, nodelist, parent=None):
     47        self.name, self.nodelist, self.parent = name, nodelist, parent
    5948
    6049    def __repr__(self):
    6150        return "<Block Node: %s. Contents: %r>" % (self.name, self.nodelist)
    6251
    6352    def render(self, context):
    6453        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
    6757        result = self.nodelist.render(context)
    6858        context.pop()
    6959        return result
    7060
     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
    7173class ExtendsNode(template.Node):
    7274    def __init__(self, nodelist, parent_name, parent_name_var, template_dirs=None):
    7375        self.nodelist = nodelist
     
    104106                if parent_is_child:
    105107                    compiled_parent.nodelist[0].nodelist.append(block_node)
    106108            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)
    109112                parent_block.nodelist = block_node.nodelist
    110113        return compiled_parent.render(context)
    111114
  • tests/othertests/templates.py

     
    185185    # {% load %} tag (within a child template)
    186186    'inheritance19': ("{% extends 'inheritance01' %}{% block first %}{% load testtags %}{% echo 400 %}5678{% endblock %}", {}, '140056783_'),
    187187
     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
    188200    ### EXCEPTIONS ############################################################
    189201
    190202    # Raise exception for invalid template name
Back to Top