diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index c46e20c1..12d21d8a 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -1089,7 +1089,7 @@ def sync_do_slice( end = offset + (slice_number + 1) * items_per_slice tmp = seq[start:end] - if fill_with is not None and slice_number >= slices_with_extra: + if fill_with is not None and slices_with_extra and slice_number >= slices_with_extra: tmp.append(fill_with) yield tmp diff --git a/tests/test_filters.py b/tests/test_filters.py index 4601469a..4ccc9f3a 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -78,6 +78,11 @@ class TestFilter: "[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]" ) + def test_slice_evenly_divisible(self, env): + tmpl = env.from_string("{{ foo|slice(4, 'X')|list }}") + out = tmpl.render(foo=[1, 2, 3, 4]) + assert out == "[[1], [2], [3], [4]]" + def test_escape(self, env): tmpl = env.from_string("""{{ '<">&'|escape }}""") out = tmpl.render()