Add the column filter

This commit is contained in:
Will Rowe
2016-05-19 18:44:50 -04:00
parent e3470f5e4f
commit 48ae9d9cbf
3 changed files with 41 additions and 1 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 2.7.5 (2019-XX-XX)
* n/a
* add "column" filter
* 2.7.4 (2019-03-23)
+29
View File
@@ -226,6 +226,7 @@ final class CoreExtension extends AbstractExtension
new TwigFilter('sort', 'twig_sort_filter'),
new TwigFilter('merge', 'twig_array_merge'),
new TwigFilter('batch', 'twig_array_batch'),
new TwigFilter('column', 'twig_array_column'),
// string/array filters
new TwigFilter('reverse', 'twig_reverse_filter', ['needs_environment' => true]),
@@ -1684,4 +1685,32 @@ function twig_get_attribute(Environment $env, Source $source, $object, $item, ar
return $ret;
}
/**
* Return the values from a single column in the input array.
*
* <pre>
* {% set items = [{ 'fruit' : 'apple'}, {'fruit' : 'orange' }] %}
*
* {% set fruits = items|column('fruit') %}
*
* {# fruits now contains ['apple', 'orange'] #}
* </pre>
*
* @param array|Traversable $arr An array
* @param mixed $column_key The column key
*
* @return array The array of values
*/
function twig_array_column($arr, $column_key)
{
if ($arr instanceof Traversable) {
$arr = \iterator_to_array($arr);
} elseif (!\is_array($arr)) {
throw new RuntimeError(sprintf('The column filter only works with arrays or "Traversable", got "%s" as first argument.', \gettype($arr)));
}
return \array_column($arr, $column_key);
}
}
@@ -0,0 +1,11 @@
--TEST--
"column" filter
--TEMPLATE--
{{ array|column('foo')|join }}
{{ traversable|column('foo')|join }}
--DATA--
$items = array(array('bar' => 'foo', 'foo' => 'bar'), array('foo' => 'foo', 'bar' => 'bar'));
return array('array' => $items, 'traversable' => new ArrayIterator($items));
--EXPECT--
barfoo
barfoo