Django

Code

Changeset 715

Show
Ignore:
Timestamp:
09/28/05 23:22:09 (3 years ago)
Author:
adrian
Message:

Fixed #501 -- Fixed block.super in multi-level templates, and added unit tests to confirm. Thanks for the patch, django@kieranholland.com

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/template_loader.py

    r664 r715  
    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, nodelis
     46    def __init__(self, name, nodelist, parent=None): 
     47        self.name, self.nodelist, self.parent = name, nodelist, paren
    5948 
    6049    def __repr__(self): 
     
    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 
     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) 
    7071 
    7172class ExtendsNode(template.Node): 
     
    105106                    compiled_parent.nodelist[0].nodelist.append(block_node) 
    106107            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) 
    109111                parent_block.nodelist = block_node.nodelist 
    110112        return compiled_parent.render(context) 
  • django/trunk/tests/othertests/templates.py

    r634 r715  
    185185    # {% load %} tag (within a child template) 
    186186    '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_'), 
    187199 
    188200    ### EXCEPTIONS ############################################################