Prevent default filter from throwing UndefinedError #138

Merged
merged 1 commit into from May 19, 2013
View
@@ -276,7 +276,7 @@ def do_default(value, default_value=u'', boolean=False):
{{ ''|default('the string was empty', true) }}
"""
- if (boolean and not value) or isinstance(value, Undefined):
+ if isinstance(value, Undefined) or (boolean and not value):
return default_value
return value
@@ -214,6 +214,7 @@ def test_strict_undefined(self):
self.assert_equal(env.from_string('{{ missing is not defined }}').render(), 'True')
self.assert_raises(UndefinedError, env.from_string('{{ foo.missing }}').render, foo=42)
self.assert_raises(UndefinedError, env.from_string('{{ not missing }}').render)
+ self.assert_equal(env.from_string('{{ missing|default("default", true) }}').render(), 'default')
def test_indexing_gives_undefined(self):
t = Template("{{ var[42].foo }}")