added Traversable objects support for the join filter

This commit is contained in:
Fabien Potencier
2012-01-02 13:10:08 +01:00
parent 956830c7ee
commit 19ba9c2141
4 changed files with 28 additions and 1 deletions
+4
View File
@@ -1,3 +1,7 @@
* 1.5.0
* added Traversable objects support for the join filter
* 1.5.0-RC2 (2011-12-30)
* added a way to set the default global date interval format
+8
View File
@@ -488,6 +488,14 @@ function twig_array_merge($arr1, $arr2)
*/
function twig_join_filter($value, $glue = '')
{
if ($value instanceof Traversable) {
$values = array();
foreach ($value as $v) {
$values[] = $v;
}
$value = $values;
}
return implode($glue, (array) $value);
}
@@ -0,0 +1,10 @@
--TEST--
"join" filter
--TEMPLATE--
{{ ["foo", "bar"]|join(', ') }}
{{ foo|join(', ') }}
--DATA--
return array('foo' => new Foo())
--EXPECT--
foo, bar
1, 2
+6 -1
View File
@@ -114,7 +114,7 @@ function test_foo($value = 'foo')
return $value;
}
class Foo
class Foo implements IteratorAggregate
{
const BAR_NAME = 'bar';
@@ -123,6 +123,11 @@ class Foo
return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : '');
}
public function getIterator()
{
return new ArrayObject(array(1, 2));
}
public function getFoo()
{
return 'foo';