fix batch filter with zero items

also added a test for preserving keys behavior
This commit is contained in:
Tobias Schultze
2015-07-02 23:47:18 +02:00
parent ae448f3ae2
commit 8d6be28430
3 changed files with 21 additions and 1 deletions
+1 -1
View File
@@ -1488,7 +1488,7 @@ function twig_array_batch($items, $size, $fill = null)
$result = array_chunk($items, $size, true);
if (null !== $fill) {
if (null !== $fill && !empty($result)) {
$last = count($result) - 1;
if ($fillCount = $size - count($result[$last])) {
$result[$last] = array_merge(
@@ -0,0 +1,10 @@
--TEST--
"batch" filter preserves array keys
--TEMPLATE--
{{ {'foo': 'bar', 'key': 'value'}|batch(4)|first|keys|join(',') }}
{{ {'foo': 'bar', 'key': 'value'}|batch(4, 'fill')|first|keys|join(',') }}
--DATA--
return array()
--EXPECT--
foo,key
foo,key,0,1
@@ -0,0 +1,10 @@
--TEST--
"batch" filter with zero elements
--TEMPLATE--
{{ []|batch(3)|length }}
{{ []|batch(3, 'fill')|length }}
--DATA--
return array()
--EXPECT--
0
0