From: Armin Ronacher Date: Sun, 7 Feb 2010 00:27:47 +0000 (+0100) Subject: Further error message improvement, this time for #341. X-Git-Tag: 2.3~29 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=92622e9145a6b50d59ac7ed30fd255a664b76f4e;p=jinja2.git Further error message improvement, this time for #341. --HG-- branch : trunk --- diff --git a/jinja2/parser.py b/jinja2/parser.py index bc4925e..96980d1 100644 --- a/jinja2/parser.py +++ b/jinja2/parser.py @@ -212,6 +212,15 @@ class Parser(object): node = nodes.Block(lineno=next(self.stream).lineno) node.name = self.stream.expect('name').value node.scoped = self.stream.skip_if('name:scoped') + + # common problem people encounter when switching from django + # to jinja. we do not support hyphens in block names, so let's + # raise a nicer error message in that case. + if self.stream.current.type == 'sub': + self.fail('Block names in Jinja have to be valid Python ' + 'identifiers and may not contain hypens, use an ' + 'underscore instead.') + node.body = self.parse_statements(('name:endblock',), drop_needle=True) self.stream.skip_if('name:' + node.name) return node diff --git a/tests/test_parser.py b/tests/test_parser.py index cb75ca6..9e546c4 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -141,3 +141,6 @@ def test_error_messages(): "Unexpected end of template. Jinja was looking for the " "following tags: 'endfor' or 'else'. The innermost block " "that needs to be closed is 'for'.") + assert_error('{% block foo-bar-baz %}', + "Block names in Jinja have to be valid Python identifiers " + "and may not contain hypens, use an underscore instead.")