summaryrefslogtreecommitdiff
path: root/jinja2
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2015-05-25 12:24:09 +0200
committerArmin Ronacher <armin.ronacher@active-4.com>2015-05-25 12:24:09 +0200
commit0c1f192670fff219ae1c61fb69c75ae4fb0fd50d (patch)
treeb7461bbda9c75636d1d1f873401ae35406258fa3 /jinja2
parent84f39ff5afd89760e1eff387ba4ce38e4a982977 (diff)
downloadjinja2-0c1f192670fff219ae1c61fb69c75ae4fb0fd50d.tar.gz
Added decorator support for finalize. This fixes #431
Diffstat (limited to 'jinja2')
-rw-r--r--jinja2/compiler.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/jinja2/compiler.py b/jinja2/compiler.py
index 5cd872d..fad007b 100644
--- a/jinja2/compiler.py
+++ b/jinja2/compiler.py
@@ -1219,8 +1219,17 @@ class CodeGenerator(NodeVisitor):
if self.has_known_extends and frame.require_output_check:
return
+ allow_constant_finalize = True
if self.environment.finalize:
- finalize = lambda x: text_type(self.environment.finalize(x))
+ func = self.environment.finalize
+ if getattr(func, 'contextfunction', False) or \
+ getattr(func, 'evalcontextfunction', False):
+ allow_constant_finalize = False
+ elif getattr(func, 'environmentfunction', False):
+ finalize = lambda x: text_type(
+ self.environment.finalize(self.environment, x))
+ else:
+ finalize = lambda x: text_type(self.environment.finalize(x))
else:
finalize = text_type
@@ -1236,6 +1245,8 @@ class CodeGenerator(NodeVisitor):
body = []
for child in node.nodes:
try:
+ if not allow_constant_finalize:
+ raise nodes.Impossible()
const = child.as_const(frame.eval_ctx)
except nodes.Impossible:
body.append(child)
@@ -1291,6 +1302,9 @@ class CodeGenerator(NodeVisitor):
self.write('to_string(')
if self.environment.finalize is not None:
self.write('environment.finalize(')
+ if getattr(self.environment.finalize,
+ "contextfunction", False):
+ self.write('context, ')
close += 1
self.visit(item, frame)
self.write(')' * close)
@@ -1313,7 +1327,6 @@ class CodeGenerator(NodeVisitor):
arguments.append(item)
self.writeline('yield ')
self.write(repr(concat(format)) + ' % (')
- idx = -1
self.indent()
for argument in arguments:
self.newline(argument)
@@ -1327,6 +1340,15 @@ class CodeGenerator(NodeVisitor):
close += 1
if self.environment.finalize is not None:
self.write('environment.finalize(')
+ if getattr(self.environment.finalize,
+ 'contextfunction', False):
+ self.write('context, ')
+ elif getattr(self.environment.finalize,
+ 'evalcontextfunction', False):
+ self.write('context.eval_ctx, ')
+ elif getattr(self.environment.finalize,
+ 'environmentfunction', False):
+ self.write('environment, ')
close += 1
self.visit(argument, frame)
self.write(')' * close + ', ')