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
Provenance
- Commit
0a2ac69- Tree
f6ed9c0- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
69e12db
8 files changed+35 added−47 removed
modified:docs/_templates/genindex.html+1 added-1 removed
122345630 unmodified lines{% extends "layout.html" %}{% title = 'Index' %}{% set title = 'Index' %}{% block body %}
<h1 id="index">Index</h1>
30 unmodified linesmodified:docs/_templates/search.html+1 added-1 removed
122345629 unmodified lines{% extends "layout.html" %}{% title = 'Search' %}{% set title = 'Search' %}{% block extrahead %} <script type="text/javascript" src="{{ pathto('_static/searchtools.js', 1) }}"></script>{% endblock %}{% block body %}29 unmodified linesmodified:jinja2/compiler.py+2 added-2 removed
785 unmodified lines786 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 linesmodified:jinja2/defaults.py+2 added-1 removed
23 unmodified lines24 25 DEFAULT_NAMESPACE = {26 'range': xrange,27 'dict': lambda **kw: kw,28- 'lipsum': generate_lorem_ipsum28+ 'lipsum': generate_lorem_ipsum,29+ 'void': lambda *a: u''30 }31 32 33 # export all constants1 unmodified linemodified:jinja2/parser.py+25 added-31 removed
11 unmodified lines12 from jinja2.exceptions import TemplateSyntaxError13 14 15 _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'])19 20 21 class Parser(object):31 unmodified lines53 return rv54 55 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)72 76- 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)86-73 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 the14 unmodified lines91 if drop_needle:92 self.stream.next()93 return result94 95+ 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)102+103 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 linesmodified:tests/test_macros.py+1 added-1 removed
38 unmodified lines39 {% call(data) test() %}{{ data }}{% endcall %}\40 '''41 42 CALLERUNDEFINED = '''\43-{% caller = 42 %}\43+{% set caller = 42 %}\44 {% macro test() %}{{ caller is not defined }}{% endmacro %}\45 {{ test() }}\46 '''47 41 unmodified linesmodified:tests/test_various.py+3 added-3 removed
11 unmodified lines12 UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''13 RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}'''14 CONST = '''{{ true }}|{{ false }}|{{ none }}|\15 {{ none is defined }}|{{ missing is defined }}'''16-LOCALSET = '''{% foo = 0 %}\17-{% for item in [1, 2] %}{% foo = 1 %}{% endfor %}\16+LOCALSET = '''{% set foo = 0 %}\17+{% for item in [1, 2] %}{% set foo = 1 %}{% endfor %}\18 {{ foo }}'''19-CONSTASS1 = '''{% true = 42 %}'''19+CONSTASS1 = '''{% set true = 42 %}'''20 CONSTASS2 = '''{% for none in seq %}{% endfor %}'''21 22 23 def test_unpacking(env):39 unmodified linesmodified:TODO-7 removed
46 unmodified lines47 unlike locals and filters/tests which are always pulled. We're not doing that48 for filters/tests/locals as nested scopes may access it and testing is too49 complicated for the tiny performance improvement but easy for attribute50 lookups, keeping the complexity of the whole thing in mind.51-52-Use `set` for Assignments53--------------------------54-55-The keyword-less way to assign variabeles is a left-over from the days when56-it was possible to use Jinja2 like a regular programming language without57-necessary template end/start delimiters to group statements.