Django

Code

Changeset 7089

Show
Ignore:
Timestamp:
02/05/08 17:41:48 (10 months ago)
Author:
mtredinnick
Message:

Tweaked [7082] and [7084] a little bit to also allow comment nodes prior to the extends tag.

This would be little less fiddly if we knew nodelist were always of type
NodeList?, but they could be normal lists. Or if we merged successive TextNodes?,
instead of appending them. Something to think about going forwards.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/__init__.py

    r7084 r7089  
    290290 
    291291    def extend_nodelist(self, nodelist, node, token): 
    292         if (node.must_be_first and nodelist and 
    293                 (not isinstance(nodelist[0], TextNode) or len(nodelist) > 2)): 
    294             raise TemplateSyntaxError("%r must be the first tag in the template." % node) 
     292        if node.must_be_first and nodelist: 
     293            try: 
     294                if nodelist.contains_nontext: 
     295                    raise AttributeError 
     296            except AttributeError: 
     297                raise TemplateSyntaxError("%r must be the first tag in the template." % node) 
     298        if isinstance(nodelist, NodeList) and not isinstance(node, TextNode): 
     299            nodelist.contains_nontext = True 
    295300        nodelist.append(node) 
    296301 
     
    733738 
    734739class NodeList(list): 
     740    # Set to True the first time a non-TextNode is inserted by 
     741    # extend_nodelist(). 
     742    contains_nontext = False 
     743 
    735744    def render(self, context): 
    736745        bits = [] 
  • django/trunk/django/template/loader_tags.py

    r7085 r7089  
    7070    def render(self, context): 
    7171        compiled_parent = self.get_parent(context) 
    72         if len(compiled_parent.nodelist) > 1: 
    73             n0, n1 = compiled_parent.nodelist[:2] 
    74         else: 
    75             n0, n1 = compiled_parent.nodelist[0], None 
    76         parent_is_child = (isinstance(n0, ExtendsNode) or 
    77                 (isinstance(n0, TextNode) and isinstance(n1, ExtendsNode))) 
    78         if parent_is_child: 
    79             extend_node = int(not isinstance(n0, ExtendsNode)) 
     72        pos = 0 
     73        while isinstance(compiled_parent.nodelist[pos], TextNode): 
     74            pos += 1 
     75        parent_is_child = isinstance(compiled_parent.nodelist[pos], ExtendsNode) 
    8076        parent_blocks = dict([(n.name, n) for n in compiled_parent.nodelist.get_nodes_by_type(BlockNode)]) 
    8177        for block_node in self.nodelist.get_nodes_by_type(BlockNode): 
     
    8985                # it'll be checked when the parent node's render() is called. 
    9086                if parent_is_child: 
    91                     compiled_parent.nodelist[extend_node].nodelist.append(block_node) 
     87                    compiled_parent.nodelist[pos].nodelist.append(block_node) 
    9288            else: 
    9389                # Keep any existing parents and add a new one. Used by BlockNode.