Skip to content
Myr

Automated merge with ssh://team@pocoo.org/jinja2-main

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 · parents 671594b, ca0666d · Browse files · 2 files+21 added−25 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+21 added−25 removed

  • other:+21 added−25 removed

2 files+21 added−25 removed

modified:jinja2/optimizer.py+13 added-22 removed
6 unmodified lines
78 * eliminating constant nodes9 * evaluating filters and macros on constant nodes10 * unroll loops on constant values11 * replace variables which are already known (because the doesn't11 * replace variables which are already known (because they doesn't12 change often and you want to prerender a template) with constants1314 After the optimation you will get a new, simplier template which can15 be saved again for later rendering. But even if you don't want to
5 unmodified lines
21"""22from copy import deepcopy23from jinja2 import nodes24from jinja2.visitor import NodeVisitor, NodeTransformer25from jinja2.runtime import subscribe262727class Optimizer(NodeVisitor):28class Optimizer(NodeTransformer):2930 def __init__(self, environment, context={}):31 self.environment = environment32 self.context = context3333 def visit_Output(self, node):34 node.nodes = [self.visit(n) for n in node.nodes]35 return node3637 def visit_Template(self, node):38 body = []39 for n in node.body:40 x = self.visit(n)41 if isinstance(x, list):42 body.extend(x)43 else:44 body.append(x)45 node.body = body46 return node4734 def visit_Filter(self, node):35 """Try to evaluate filters if possible."""36 try:37 x = self.visit(node.node).as_const()
24 unmodified lines
62 if node.name not in self.context:63 return node64 return nodes.Const(self.context[node.name])6580 def generic_visit(self, node, *args, **kwargs):81 NodeVisitor.generic_visit(self, node, *args, **kwargs)82 return node66 def visit_Subscript(self, node):67 try:68 item = self.visit(node.node).as_const()69 arg = self.visit(node.arg).as_const()70 except nodes.Impossible:71 return node72 # XXX: what does the 3rd parameter mean?73 return nodes.Const(subscribe(item, arg, None))747585def optimize(node, environment):86 optimizer = Optimizer(environment)76def optimize(node, environment, context={}):77 optimizer = Optimizer(environment, context=context)78 return optimizer.visit(node)
modified:test_optimizer.py+8 added-3 removed
2 unmodified lines
3from jinja2.optimizer import optimize456env = Environment()7forums = [8 {'id': 1, 'name': u'Example'},9 {'id': 2, 'name': u'Foobar'},10 {'id': 3, 'name': u'<42>'}11]12ast = env.parse("""13 Hi {{ "<blub>"|e }},14 how are you?1511 {% for item in ('foo', 'bar', 'blub') %}12 {{ item }}16 {% for forum in forums %}17 {{ readstatus(forum.id) }} {{ forum.id|e }} {{ forum.name|e }}18 {% endfor %}19""")20print ast21print22print generate(ast, env, "foo.html")23print19ast = optimize(ast, env)24ast = optimize(ast, env, context={'forums': forums})25print ast26print27print generate(ast, env, "foo.html")