Skip to content

Commit

Permalink
fixed batch filter clobbers array keys when fill parameter is used
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 12, 2019
1 parent 37c398f commit eff91c5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions 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
Expand Down
4 changes: 3 additions & 1 deletion src/Extension/CoreExtension.php
Expand Up @@ -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;
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions 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') %}
<div class=row>
{% for key, column in row %}
<div class={{ key }}>{{ column }}</div>
{% endfor %}
</div>
{% endfor %}
--DATA--
return ['items' => ['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', '123' => 'e']]
--EXPECT--
<div class=row>
<div class=a>a</div>
<div class=b>b</div>
<div class=c>c</div>
</div>
<div class=row>
<div class=d>d</div>
<div class=123>e</div>
<div class=124>fill</div>
</div>

0 comments on commit eff91c5

Please sign in to comment.