Skip to content
Myr

switched back to explicit set for assignments. {% foo = 42 %} becomes {% set foo = 42 %} and {% foo.something() }} becomes {{ void(foo.something() }} with this commit.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 69e12db · Browse files · 8 files+35 added−47 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+35 added−47 removed

  • tests:+4 added−4 removed
  • other:+31 added−43 removed

8 files+35 added−47 removed

modified:docs/_templates/genindex.html+1 added-1 removed
modified:docs/_templates/search.html+1 added-1 removed
modified:jinja2/compiler.py+2 added-2 removed
785 unmodified lines
786 self.writeline('if l_%s is missing:' % alias)787 self.indent()788 self.writeline('l_%s = environment.undefined(%r %% '789 'included_template.name, '790 'name=included_template.name)' %790 'name=%r)' %791 (alias, 'the template %r does not export '792 'the requested name ' + repr(name)))792 'the requested name ' + repr(name), name))793 self.outdent()794 if frame.toplevel:795 self.writeline('context.vars[%r] = l_%s' % (alias, alias))796 if not alias.startswith('__'):
557 unmodified lines
modified:jinja2/defaults.py+2 added-1 removed
23 unmodified lines
2425DEFAULT_NAMESPACE = {26 'range': xrange,27 'dict': lambda **kw: kw,28 'lipsum': generate_lorem_ipsum28 'lipsum': generate_lorem_ipsum,29 'void': lambda *a: u''30}313233# export all constants
1 unmodified line
modified:jinja2/parser.py+25 added-31 removed
11 unmodified lines
12from jinja2.exceptions import TemplateSyntaxError131415_statement_keywords = frozenset(['for', 'if', 'block', 'extends', 'print',16 'macro', 'include', 'from', 'import'])16 'macro', 'include', 'from', 'import',17 'set'])18_compare_operators = frozenset(['eq', 'ne', 'lt', 'lteq', 'gt', 'gteq'])192021class Parser(object):
31 unmodified lines
53 return rv5455 def parse_statement(self):56 """Parse a single statement."""56 token_type = self.stream.current.type57 if self.stream.current.type is 'name':58 if self.stream.current.value in _statement_keywords:59 return getattr(self, 'parse_' + self.stream.current.value)()60 elif self.stream.current.value == 'call':61 return self.parse_call_block()62 elif self.stream.current.value == 'filter':63 return self.parse_filter_block()64 else:65 ext = self.extensions.get(self.stream.current.value)66 if ext is not None:67 return ext(self)68 lineno = self.stream.current.lineno69 expr = self.parse_tuple()70 if self.stream.current.type == 'assign':71 result = self.parse_assign(expr)72 else:73 result = nodes.ExprStmt(expr, lineno=lineno)74 return result57 token = self.stream.current58 if token.type is not 'name':59 raise TemplateSyntaxError('tag name expected', token.lineno,60 self.filename)61 if token.value in _statement_keywords:62 return getattr(self, 'parse_' + self.stream.current.value)()63 if token.value == 'call':64 return self.parse_call_block()65 if token.value == 'filter':66 return self.parse_filter_block()67 ext = self.extensions.get(token.value)68 if ext is not None:69 return ext(self)70 raise TemplateSyntaxError('unknown tag %r' % token.value,71 token.lineno, self.filename)7276 def parse_assign(self, target):77 """Parse an assign statement."""78 lineno = self.stream.expect('assign').lineno79 if not target.can_assign():80 raise TemplateSyntaxError("can't assign to '%s'" %81 target.__class__.__name__.lower(),82 target.lineno, self.filename)83 expr = self.parse_tuple()84 target.set_ctx('store')85 return nodes.Assign(target, expr, lineno=lineno)8673 def parse_statements(self, end_tokens, drop_needle=False):74 """Parse multiple statements into a list until one of the end tokens75 is reached. This is used to parse the body of statements as it also76 parses template data if appropriate. The parser checks first if the
14 unmodified lines
91 if drop_needle:92 self.stream.next()93 return result9495 def parse_set(self):96 """Parse an assign statement."""97 lineno = self.stream.next().lineno98 target = self.parse_assign_target()99 self.stream.expect('assign')100 expr = self.parse_tuple()101 return nodes.Assign(target, expr, lineno=lineno)102103 def parse_for(self):104 """Parse a for loop."""105 lineno = self.stream.expect('name:for').lineno106 target = self.parse_assign_target(extra_end_rules=('name:in',))
648 unmodified lines
modified:tests/test_macros.py+1 added-1 removed
38 unmodified lines
39{% call(data) test() %}{{ data }}{% endcall %}\40'''4142CALLERUNDEFINED = '''\43{% caller = 42 %}\43{% set caller = 42 %}\44{% macro test() %}{{ caller is not defined }}{% endmacro %}\45{{ test() }}\46'''47
41 unmodified lines
modified:tests/test_various.py+3 added-3 removed
11 unmodified lines
12UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''13RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}'''14CONST = '''{{ true }}|{{ false }}|{{ none }}|\15{{ none is defined }}|{{ missing is defined }}'''16LOCALSET = '''{% foo = 0 %}\17{% for item in [1, 2] %}{% foo = 1 %}{% endfor %}\16LOCALSET = '''{% set foo = 0 %}\17{% for item in [1, 2] %}{% set foo = 1 %}{% endfor %}\18{{ foo }}'''19CONSTASS1 = '''{% true = 42 %}'''19CONSTASS1 = '''{% set true = 42 %}'''20CONSTASS2 = '''{% for none in seq %}{% endfor %}'''212223def test_unpacking(env):
39 unmodified lines
modified:TODO-7 removed
46 unmodified lines
47unlike locals and filters/tests which are always pulled. We're not doing that48for filters/tests/locals as nested scopes may access it and testing is too49complicated for the tiny performance improvement but easy for attribute50lookups, keeping the complexity of the whole thing in mind.5152Use `set` for Assignments53-------------------------5455The keyword-less way to assign variabeles is a left-over from the days when56it was possible to use Jinja2 like a regular programming language without57necessary template end/start delimiters to group statements.