diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index c46e20c10..394fb4ab3 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -847,6 +847,7 @@ def do_indent( indention = Markup(indention) newline = Markup(newline) + is_empty = not s s += newline # this quirk is necessary for splitlines method if blank: @@ -860,7 +861,7 @@ def do_indent( indention + line if line else line for line in lines ) - if first: + if first and (not is_empty or blank): rv = indention + rv return rv diff --git a/tests/test_filters.py b/tests/test_filters.py index 4601469a6..dd37df763 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -179,6 +179,10 @@ def test_indent(self, env): t = env.from_string('{{ "jinja"|indent(blank=true) }}') assert t.render() == "jinja" + def test_indent_empty_string_with_first(self, env): + t = env.from_string('{{ ""|indent(first=true) }}') + assert t.render() == "" + def test_indent_markup_input(self, env): """ Tests cases where the filter input is a Markup type