fixed the filter filter

This commit is contained in:
Fabien Potencier
2019-06-14 07:36:32 +02:00
parent b957ff6f29
commit 29ad145dd8
3 changed files with 13 additions and 4 deletions
+1
View File
@@ -1,5 +1,6 @@
* 2.11.3 (2019-XX-XX)
* fixed the filter filter (allow the result to be used several times)
* fixed macro auto-import when a template contains only macros
* 2.11.2 (2019-06-05)
+5 -4
View File
@@ -1522,11 +1522,12 @@ function twig_array_column($array, $name): array
function twig_array_filter($array, $arrow)
{
foreach ($array as $k => $v) {
if ($arrow($v, $k)) {
yield $k => $v;
}
if (\is_array($array)) {
return array_filter($array, $arrow, \ARRAY_FILTER_USE_BOTH);
}
// the IteratorIterator wrapping is needed as some internal PHP classes are \Traversable but do not implement \Iterator
return new \CallbackFilterIterator(new \IteratorIterator($array), $arrow);
}
function twig_array_map($array, $arrow)
@@ -35,6 +35,11 @@
{% for k, v in xml|filter(x => true) %}
{{ k }}/{{ v }}
{% endfor %}
{% set coll = ['a', 'b']|filter(v => v is same as('a')) %}
{% if coll|length > 0 %}
{{- coll|join(', ') }}
{% endif %}
--DATA--
return [
'it' => new \ArrayIterator(['a' => 1, 'b' => 2, 'c' => 5, 'd' => 8]),
@@ -68,3 +73,5 @@ elem/baz
elem/foo
elem/bar
elem/baz
a