Skip to content
Myr

[svn] constants are now immutable in jinja

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 2acbac1 · Browse files · 2 files+70 added−14 removed
This commit

State

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

Diff stats

2 files+70 added−14 removed

  • other:+70 added−14 removed

2 files+70 added−14 removed

modified:docs/src/designerdoc.txt+25 added-1 removed
380 unmodified lines
381 ``/`` divide the left operand by the right one.382 ``{{ 1 / 2 }}`` would return ``0.5``.383 ``//`` divide the left operand by the right one and return a truncated384 integer result: ``{{ 20 // 7 }}`` is ``2``. (*new in385 Jinja 1.1*)385 Jinja 1.1*)386 ``*`` multiply the left operand with the right one.387 ``{{ 2 * 2 }}`` would return ``4``.388 ``**`` raise the left operand to the power of the right389 operand. ``{{ 2**3 }}`` would return ``8``.
395 unmodified lines
785means that they won't raise an error for several releases but yield warnings786on the application side. But it's very unlikely that new keywords will be787added.788789Global Constants790================791792Jinja provides some special variables which cannot be overridden in templates.793Those are not affected by the variable lookup:794795 `_`, `true`, `false`, `none`, `undefined`796797You cannot use those names for your own variables or macros. It's however no798problem to have blocks with that name.799800**Notice** due to a bug in Jinja 1.0 it was possible to override `_`, `true`,801`false`, `none` and `undefined` from the template by either using `{% set %}`802or in a for loop. While overriding `true`, `false` etc just caused nothing803overriding the translation marker `_` would crash the translation interface.804If you want to use `_` for unpacking in for loops (eg. skipping items) you805have to give it a proper name:806807.. sourcecode:: jinja808809 {% for key, ignore in mydict|dictsort %}810 {{ key|e }}811 {% endfor %}812813Whitespace814==========815816If the Jinja environment was created with ``trim_blocks`` set to true the
96 unmodified lines
modified:jinja/translators/python.py+45 added-13 removed
178 unmodified lines
179 nodes.Include: self.handle_include,180 nodes.Trans: self.handle_trans,181 # python nodes182 ast.Name: self.handle_name,183 ast.AssName: self.handle_name,183 ast.AssName: self.handle_assign_name,184 ast.Compare: self.handle_compare,185 ast.Const: self.handle_const,186 ast.Subscript: self.handle_subscript,187 ast.Getattr: self.handle_getattr,
80 unmodified lines
268 if n.node.__class__ is not ast.Name:269 raise TemplateSyntaxError('invalid filter. filter must '270 'be a hardcoded function name '271 'from the filter namespace',272 n.lineno)272 n.lineno,273 n.filename)274 args = []275 for arg in n.args:276 if arg.__class__ is ast.Keyword:277 raise TemplateSyntaxError('keyword arguments for '278 'filters are not supported.',278 n.lineno)279 n.lineno,280 n.filename)281 args.append(self.handle_node(arg))282 if n.star_args is not None or n.dstar_args is not None:283 raise TemplateSyntaxError('*args / **kwargs is not supported '282 'for filters', n.lineno)284 'for filters', n.lineno,285 n.filename)286 filters.append('(%r, %s)' % (287 n.node.name,288 _to_tuple(args)289 ))
2 unmodified lines
292 else:293 raise TemplateSyntaxError('invalid filter. filter must be a '294 'hardcoded function name from the '295 'filter namespace',293 n.lineno)296 n.lineno, n.filename)297 return 'apply_filters(%s, context, %s)' % (s, _to_tuple(filters))298299 def handle_node(self, node):300 """
5 unmodified lines
306 out = self.handlers[node.__class__](node)307 elif node.__class__ in self.unsupported:308 raise TemplateSyntaxError('unsupported syntax element %r found.'309 % self.unsupported[node.__class__],307 node.lineno)310 node.lineno, node.filename)311 else:312 raise AssertionError('unhandled node %r' % node.__class__)313 return out314
311 unmodified lines
626 def handle_macro(self, node):627 """628 Handle macro declarations.629 """630 # sanity check for name631 if node.name == '_' or \632 node.name in self.constants:633 raise TemplateSyntaxError('cannot override %r' % node.name,634 node.lineno, node.filename)635 if node.name in self.constants:636 raise TemplateSyntaxError('you cannot name a macro %r',637 node.lineno,638 node.filename)639640 buf = []641 write = lambda x: buf.append(self.indent(x))642643 write('def macro(*args):')
30 unmodified lines
674 def handle_set(self, node):675 """676 Handle variable assignments.677 """678 if node.name == '_' or node.name in self.constants:679 raise TemplateSyntaxError('cannot override %r' % node.name,680 node.lineno, node.filename)681 return self.indent(self.nodeinfo(node)) + '\n' + \682 self.indent('context[%r] = %s' % (683 node.name,684 self.handle_node(node.expr)
74 unmodified lines
759 ))760761 # -- python nodes762763 def handle_assign_name(self, node):764 """765 Handle name assignments.766 """767 if node.name == '_' or \768 node.name in self.constants:769 raise TemplateSyntaxError('cannot override %r' % node.name,770 node.lineno, node.filename)771 return 'context[%r]' % node.name772773 def handle_name(self, node):774 """775 Handle name assignments and name retreivement.776 """
13 unmodified lines
790 # be the only operator791 if node.ops[0][0] in ('is', 'is not'):792 if len(node.ops) > 1:793 raise TemplateSyntaxError('is operator must not be chained',768 node.lineno)794 node.lineno,795 node.filename)796 elif node.ops[0][1].__class__ is ast.Name:797 args = []798 name = node.ops[0][1].name799 elif node.ops[0][1].__class__ is ast.CallFunc:
1 unmodified line
801 if n.node.__class__ is not ast.Name:802 raise TemplateSyntaxError('invalid test. test must '803 'be a hardcoded function name '804 'from the test namespace',778 n.lineno)805 n.lineno,806 n.filename)807 name = n.node.name808 args = []809 for arg in n.args:810 if arg.__class__ is ast.Keyword:811 raise TemplateSyntaxError('keyword arguments for '812 'tests are not supported.',785 n.lineno)813 n.lineno,814 n.filename)815 args.append(self.handle_node(arg))816 if n.star_args is not None or n.dstar_args is not None:817 raise TemplateSyntaxError('*args / **kwargs is not supported '789 'for tests', n.lineno)818 'for tests', n.lineno,819 n.filename)820 else:821 raise TemplateSyntaxError('is operator requires a test name'792 ' as operand', node.lineno)822 ' as operand', node.lineno,823 node.filename)824 return 'perform_test(context, %r, %s, %s, %s)' % (825 name,826 _to_tuple(args),827 self.handle_node(node.expr),
5 unmodified lines
833 buf.append(self.handle_node(node.expr))834 for op, n in node.ops:835 if op in ('is', 'is not'):836 raise TemplateSyntaxError('is operator must not be chained',806 node.lineno)837 node.lineno, node.filename)838 buf.append(op)839 buf.append(self.handle_node(n))840 return ' '.join(buf)841
8 unmodified lines
850 Handle variable based attribute access foo['bar'].851 """852 if len(node.subs) != 1:853 raise TemplateSyntaxError('attribute access requires one argument',823 node.lineno)854 node.lineno,855 node.filename)856 assert node.flags != 'OP_DELETE', 'wtf? do we support that?'857 if node.subs[0].__class__ is ast.Sliceobj:858 return '%s[%s]' % (859 self.handle_node(node.expr),
198 unmodified lines