Ticket #6544: django-allow-comments-before-extends.diff

File django-allow-comments-before-extends.diff, 1.8 KB (added by Bastian Kleineidam <calvin@…>, 17 years ago)
  • django/template/__init__.py

    commit dcbff38c016c824c5927b1f57385d19aad4157f3
    Author: Bastian Kleineidam <calvin@debian.org>
    Date:   Wed Feb 6 11:34:56 2008 +0100
    
        Allow comments and whitespace before an {% extends %}
        
    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index 1772b89..c85f9fa 100644
    a b class Parser(object):  
    303303        return NodeList()
    304304
    305305    def extend_nodelist(self, nodelist, node, token):
    306         if node.must_be_first and nodelist:
    307             try:
    308                 if nodelist.contains_nontext:
    309                     raise AttributeError
    310             except AttributeError:
    311                 raise TemplateSyntaxError("%r must be the first tag in the template." % node)
     306        if node.must_be_first and nodelist_extends_filter(nodelist):
     307            raise TemplateSyntaxError("%r must be the first tag in the template (preceding nodes: %s)" % (node, nodelist))
    312308        if isinstance(nodelist, NodeList) and not isinstance(node, TextNode):
    313309            nodelist.contains_nontext = True
    314310        nodelist.append(node)
    class Parser(object):  
    360356        else:
    361357            raise TemplateSyntaxError("Invalid filter: '%s'" % filter_name)
    362358
     359
     360is_whitespace = re.compile(r"^\s+$").search
     361def nodelist_extends_filter (nodelist):
     362    """Filter ignorable nodes preceding a "extends" tag."""
     363    return filter(lambda x: not node_extends_ignore(x), nodelist)
     364
     365
     366def node_extends_ignore (node):
     367    """Determine if the contents of the node can be ignored preceding
     368    a "extends" tag."""
     369    from defaulttags import CommentNode
     370    return (isinstance(node, TextNode) and is_whitespace(node.s)) or \
     371           isinstance(node, CommentNode)
     372
     373
    363374class TokenParser(object):
    364375    """
    365376    Subclass this and implement the top() method to parse a template line. When
Back to Top