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
Provenance
- Commit
149aa4e- Tree
fef54d8- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
671594b- Parent 2
ca0666d
2 files changed+21 added−25 removed
modified:jinja2/optimizer.py+13 added-22 removed
6 unmodified lines7 8 * 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 constants13 14 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 to5 unmodified lines21 """22 from copy import deepcopy23 from jinja2 import nodes24 from jinja2.visitor import NodeVisitor, NodeTransformer25+from jinja2.runtime import subscribe26 27 27-class Optimizer(NodeVisitor):28+class Optimizer(NodeTransformer):29 30 def __init__(self, environment, context={}):31 self.environment = environment32 self.context = context33 33- def visit_Output(self, node):34- node.nodes = [self.visit(n) for n in node.nodes]35- return node36-37- 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 node47-34 def visit_Filter(self, node):35 """Try to evaluate filters if possible."""36 try:37 x = self.visit(node.node).as_const()24 unmodified lines62 if node.name not in self.context:63 return node64 return nodes.Const(self.context[node.name])65 80- 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))74 75 85-def optimize(node, environment):86- optimizer = Optimizer(environment)76+def 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 lines3 from jinja2.optimizer import optimize4 5 6 env = Environment()7+forums = [8+ {'id': 1, 'name': u'Example'},9+ {'id': 2, 'name': u'Foobar'},10+ {'id': 3, 'name': u'<42>'}11+]12 ast = env.parse("""13 Hi {{ "<blub>"|e }},14 how are you?15 11- {% 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 """)20 print ast21 print22 print generate(ast, env, "foo.html")23 print19-ast = optimize(ast, env)24+ast = optimize(ast, env, context={'forums': forums})25 print ast26 print27 print generate(ast, env, "foo.html")