Fixed #5124 -- Added a reasonable error when "extends" is not the first template tag. Patch from k0001.

Refs #6274.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7084 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-02-04 04:52:37 +00:00
parent 12ed18935b
commit b9e088ae14
2 changed files with 14 additions and 0 deletions

View File

@ -289,6 +289,9 @@ class Parser(object):
return NodeList() return NodeList()
def extend_nodelist(self, nodelist, node, token): def extend_nodelist(self, nodelist, node, token):
if (node.must_be_first and nodelist and
(not isinstance(nodelist[0], TextNode) or len(nodelist) > 2)):
raise TemplateSyntaxError("%r must be the first tag in the template." % node)
nodelist.append(node) nodelist.append(node)
def enter_command(self, command, token): def enter_command(self, command, token):
@ -708,6 +711,10 @@ class Variable(object):
return current return current
class Node(object): class Node(object):
# Set this to True for nodes that must be first in the template (although
# they can be preceded by text nodes.
must_be_first = False
def render(self, context): def render(self, context):
"Return the node rendered as a string" "Return the node rendered as a string"
pass pass

View File

@ -37,11 +37,18 @@ class BlockNode(Node):
self.parent = BlockNode(self.name, nodelist) self.parent = BlockNode(self.name, nodelist)
class ExtendsNode(Node): class ExtendsNode(Node):
must_be_first = True
def __init__(self, nodelist, parent_name, parent_name_expr, template_dirs=None): def __init__(self, nodelist, parent_name, parent_name_expr, template_dirs=None):
self.nodelist = nodelist self.nodelist = nodelist
self.parent_name, self.parent_name_expr = parent_name, parent_name_expr self.parent_name, self.parent_name_expr = parent_name, parent_name_expr
self.template_dirs = template_dirs self.template_dirs = template_dirs
def __repr__(self):
if self.parent_name_expr:
return "<ExtendsNode: extends %s>" % self.parent_name_expr.token
return '<ExtendsNode: extends "%s">' % self.parent_name
def get_parent(self, context): def get_parent(self, context):
if self.parent_name_expr: if self.parent_name_expr:
self.parent_name = self.parent_name_expr.resolve(context) self.parent_name = self.parent_name_expr.resolve(context)