Permalink
Browse files

Fixed bug in finalizer

--HG--
branch : trunk
  • Loading branch information...
1 parent 80b7895 commit 665bfb8c95926ae92bd37cb58b783dcdf6855d2c @mitsuhiko mitsuhiko committed Jul 14, 2008
Showing with 21 additions and 1 deletion.
  1. +2 −0 CHANGES
  2. +6 −1 jinja2/compiler.py
  3. +13 −0 tests/test_various.py
View
@@ -30,6 +30,8 @@ Version 2.0
- `filesizeformat` filter uses decimal prefixes now per default and can be
set to binary mode with the second parameter.
+- fixed bug in finalizer
+
Version 2.0rc1
--------------
(no codename, released on June 9th 2008)
View
@@ -1045,6 +1045,11 @@ def visit_Output(self, node, frame):
if self.has_known_extends and frame.toplevel:
return
+ if self.environment.finalize:
+ finalize = lambda x: unicode(self.environment.finalize(x))
+ else:
+ finalize = unicode
+
self.newline(node)
# if we are in the toplevel scope and there was already an extends
@@ -1071,7 +1076,7 @@ def visit_Output(self, node, frame):
const = const.__html__()
else:
const = escape(const)
- const = unicode(const)
+ const = finalize(const)
except:
# if something goes wrong here we evaluate the node
# at runtime for easier debugging
View
@@ -71,3 +71,16 @@ def test_item_and_attribute():
assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
tmpl = env.from_string('{{ foo["items"] }}')
assert tmpl.render(foo={'items': 42}) == '42'
+
+
+def test_finalizer():
+ from jinja2 import Environment
+ def finalize_none_empty(value):
+ if value is None:
+ value = u''
+ return value
+ env = Environment(finalize=finalize_none_empty)
+ tmpl = env.from_string('{% for item in seq %}|{{ item }}{% endfor %}')
+ assert tmpl.render(seq=(None, 1, "foo")) == '||1|foo'
+ tmpl = env.from_string('<{{ none }}>')
+ assert tmpl.render() == '<>'

0 comments on commit 665bfb8

Please sign in to comment.