diff --git a/CHANGELOG b/CHANGELOG index 153e5353d6..a03df24d0a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.38.0 (2019-XX-XX) + * fixed batch filter clobbers array keys when fill parameter is used * added preserveKeys support for the batch filter * added the possibility to pass a TemplateWrapper to Twig\Environment::load() * improved the performance of the sandbox diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 7918e1f8e8..5f6e22cb74 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1651,7 +1651,9 @@ function twig_array_batch($items, $size, $fill = null, $preserveKeys = true) if (null !== $fill && $result) { $last = \count($result) - 1; if ($fillCount = $size - \count($result[$last])) { - $result[$last] = array_merge($result[$last], array_fill(0, $fillCount, $fill)); + foreach (array_fill(0, $fillCount, $fill) as $v) { + $result[$last][] = $v; + } } } diff --git a/test/Twig/Tests/Fixtures/filters/batch_with_more_elements.test b/test/Twig/Tests/Fixtures/filters/batch_with_more_elements.test new file mode 100644 index 0000000000..90f4de6859 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/batch_with_more_elements.test @@ -0,0 +1,23 @@ +--TEST-- +"batch" filter +--TEMPLATE-- +{% for row in items|batch(3, 'fill') %} +
+ {% for key, column in row %} +
{{ column }}
+ {% endfor %} +
+{% endfor %} +--DATA-- +return ['items' => ['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', '123' => 'e']] +--EXPECT-- +
+
a
+
b
+
c
+
+
+
d
+
e
+
fill
+