Skip to content

Commit 1107684

Browse files
committed
bug #3065 Fix the filter filter (fabpot)
This PR was merged into the 2.x branch. Discussion ---------- Fix the filter filter closes #3063 Commits ------- 29ad145 fixed the filter filter
2 parents b957ff6 + 29ad145 commit 1107684

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
* 2.11.3 (2019-XX-XX)
22

3+
* fixed the filter filter (allow the result to be used several times)
34
* fixed macro auto-import when a template contains only macros
45

56
* 2.11.2 (2019-06-05)

src/Extension/CoreExtension.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,11 +1522,12 @@ function twig_array_column($array, $name): array
15221522

15231523
function twig_array_filter($array, $arrow)
15241524
{
1525-
foreach ($array as $k => $v) {
1526-
if ($arrow($v, $k)) {
1527-
yield $k => $v;
1528-
}
1525+
if (\is_array($array)) {
1526+
return array_filter($array, $arrow, \ARRAY_FILTER_USE_BOTH);
15291527
}
1528+
1529+
// the IteratorIterator wrapping is needed as some internal PHP classes are \Traversable but do not implement \Iterator
1530+
return new \CallbackFilterIterator(new \IteratorIterator($array), $arrow);
15301531
}
15311532

15321533
function twig_array_map($array, $arrow)

test/Twig/Tests/Fixtures/filters/filter.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
{% for k, v in xml|filter(x => true) %}
3636
{{ k }}/{{ v }}
3737
{% endfor %}
38+
39+
{% set coll = ['a', 'b']|filter(v => v is same as('a')) %}
40+
{% if coll|length > 0 %}
41+
{{- coll|join(', ') }}
42+
{% endif %}
3843
--DATA--
3944
return [
4045
'it' => new \ArrayIterator(['a' => 1, 'b' => 2, 'c' => 5, 'd' => 8]),
@@ -68,3 +73,5 @@ elem/baz
6873
elem/foo
6974
elem/bar
7075
elem/baz
76+
77+
a

0 commit comments

Comments
 (0)