From: Armin Ronacher Date: Mon, 12 May 2008 23:03:08 +0000 (+0200) Subject: switched back to explicit set for assignments. {% foo = 42 %} becomes {% set foo... X-Git-Tag: 2.0rc1~67 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0a2ac69bb97370591306f396b693b93af67726fd;p=jinja2.git switched back to explicit set for assignments. {% foo = 42 %} becomes {% set foo = 42 %} and {% foo.something() }} becomes {{ void(foo.something() }} with this commit. --HG-- branch : trunk --- diff --git a/TODO b/TODO index 6881b76..2a022f4 100644 --- a/TODO +++ b/TODO @@ -48,10 +48,3 @@ unlike locals and filters/tests which are always pulled. We're not doing that for filters/tests/locals as nested scopes may access it and testing is too complicated for the tiny performance improvement but easy for attribute lookups, keeping the complexity of the whole thing in mind. - -Use `set` for Assignments -------------------------- - -The keyword-less way to assign variabeles is a left-over from the days when -it was possible to use Jinja2 like a regular programming language without -necessary template end/start delimiters to group statements. diff --git a/docs/_templates/genindex.html b/docs/_templates/genindex.html index e31a578..9add6e9 100644 --- a/docs/_templates/genindex.html +++ b/docs/_templates/genindex.html @@ -1,5 +1,5 @@ {% extends "layout.html" %} -{% title = 'Index' %} +{% set title = 'Index' %} {% block body %}

Index

diff --git a/docs/_templates/search.html b/docs/_templates/search.html index ab93b75..0c942b7 100644 --- a/docs/_templates/search.html +++ b/docs/_templates/search.html @@ -1,5 +1,5 @@ {% extends "layout.html" %} -{% title = 'Search' %} +{% set title = 'Search' %} {% block extrahead %} {% endblock %} diff --git a/jinja2/compiler.py b/jinja2/compiler.py index cb0da5a..4c250b5 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -787,9 +787,9 @@ class CodeGenerator(NodeVisitor): self.indent() self.writeline('l_%s = environment.undefined(%r %% ' 'included_template.name, ' - 'name=included_template.name)' % + 'name=%r)' % (alias, 'the template %r does not export ' - 'the requested name ' + repr(name))) + 'the requested name ' + repr(name), name)) self.outdent() if frame.toplevel: self.writeline('context.vars[%r] = l_%s' % (alias, alias)) diff --git a/jinja2/defaults.py b/jinja2/defaults.py index e124930..62dfda2 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -25,7 +25,8 @@ LINE_STATEMENT_PREFIX = None DEFAULT_NAMESPACE = { 'range': xrange, 'dict': lambda **kw: kw, - 'lipsum': generate_lorem_ipsum + 'lipsum': generate_lorem_ipsum, + 'void': lambda *a: u'' } diff --git a/jinja2/parser.py b/jinja2/parser.py index 941e037..f2a129c 100644 --- a/jinja2/parser.py +++ b/jinja2/parser.py @@ -13,7 +13,8 @@ from jinja2.exceptions import TemplateSyntaxError _statement_keywords = frozenset(['for', 'if', 'block', 'extends', 'print', - 'macro', 'include', 'from', 'import']) + 'macro', 'include', 'from', 'import', + 'set']) _compare_operators = frozenset(['eq', 'ne', 'lt', 'lteq', 'gt', 'gteq']) @@ -53,36 +54,21 @@ class Parser(object): def parse_statement(self): """Parse a single statement.""" - token_type = self.stream.current.type - if self.stream.current.type is 'name': - if self.stream.current.value in _statement_keywords: - return getattr(self, 'parse_' + self.stream.current.value)() - elif self.stream.current.value == 'call': - return self.parse_call_block() - elif self.stream.current.value == 'filter': - return self.parse_filter_block() - else: - ext = self.extensions.get(self.stream.current.value) - if ext is not None: - return ext(self) - lineno = self.stream.current.lineno - expr = self.parse_tuple() - if self.stream.current.type == 'assign': - result = self.parse_assign(expr) - else: - result = nodes.ExprStmt(expr, lineno=lineno) - return result - - def parse_assign(self, target): - """Parse an assign statement.""" - lineno = self.stream.expect('assign').lineno - if not target.can_assign(): - raise TemplateSyntaxError("can't assign to '%s'" % - target.__class__.__name__.lower(), - target.lineno, self.filename) - expr = self.parse_tuple() - target.set_ctx('store') - return nodes.Assign(target, expr, lineno=lineno) + token = self.stream.current + if token.type is not 'name': + raise TemplateSyntaxError('tag name expected', token.lineno, + self.filename) + if token.value in _statement_keywords: + return getattr(self, 'parse_' + self.stream.current.value)() + if token.value == 'call': + return self.parse_call_block() + if token.value == 'filter': + return self.parse_filter_block() + ext = self.extensions.get(token.value) + if ext is not None: + return ext(self) + raise TemplateSyntaxError('unknown tag %r' % token.value, + token.lineno, self.filename) def parse_statements(self, end_tokens, drop_needle=False): """Parse multiple statements into a list until one of the end tokens @@ -106,6 +92,14 @@ class Parser(object): self.stream.next() return result + def parse_set(self): + """Parse an assign statement.""" + lineno = self.stream.next().lineno + target = self.parse_assign_target() + self.stream.expect('assign') + expr = self.parse_tuple() + return nodes.Assign(target, expr, lineno=lineno) + def parse_for(self): """Parse a for loop.""" lineno = self.stream.expect('name:for').lineno diff --git a/tests/test_macros.py b/tests/test_macros.py index 74594d3..5958250 100644 --- a/tests/test_macros.py +++ b/tests/test_macros.py @@ -40,7 +40,7 @@ COMPLEXCALL = '''\ ''' CALLERUNDEFINED = '''\ -{% caller = 42 %}\ +{% set caller = 42 %}\ {% macro test() %}{{ caller is not defined }}{% endmacro %}\ {{ test() }}\ ''' diff --git a/tests/test_various.py b/tests/test_various.py index 37082ba..9235a29 100644 --- a/tests/test_various.py +++ b/tests/test_various.py @@ -13,10 +13,10 @@ UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}''' CONST = '''{{ true }}|{{ false }}|{{ none }}|\ {{ none is defined }}|{{ missing is defined }}''' -LOCALSET = '''{% foo = 0 %}\ -{% for item in [1, 2] %}{% foo = 1 %}{% endfor %}\ +LOCALSET = '''{% set foo = 0 %}\ +{% for item in [1, 2] %}{% set foo = 1 %}{% endfor %}\ {{ foo }}''' -CONSTASS1 = '''{% true = 42 %}''' +CONSTASS1 = '''{% set true = 42 %}''' CONSTASS2 = '''{% for none in seq %}{% endfor %}'''