Skip to content

Commit

Permalink
gracefully handle None in nl2br filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred Wenzel committed Mar 25, 2010
1 parent d6db9e6 commit 9e8d1ad
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions jingo/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def f(string, *args, **kwargs):
@register.filter
def nl2br(string):
"""Turn newlines into <br>."""
if not string:
return ''
return jinja2.Markup('<br>'.join(jinja2.escape(string).splitlines()))


Expand Down
4 changes: 4 additions & 0 deletions jingo/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def test_nl2br():
s = render('{{ x|nl2br }}', {'x': text})
eq_(s, "some<br>text<br><br>with<br>newlines")

text = None
s = render('{{ x|nl2br }}', {'x': text})
eq_(s, '')


def test_datetime():
time = datetime(2009, 12, 25, 10, 11, 12)
Expand Down

0 comments on commit 9e8d1ad

Please sign in to comment.