diff --git a/CHANGELOG b/CHANGELOG index d4e1e6944..360eaf7d6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 50834120a..a042c3794 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -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); } diff --git a/test/Twig/Tests/Fixtures/filters/join.test b/test/Twig/Tests/Fixtures/filters/join.test new file mode 100644 index 000000000..244fa104a --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/join.test @@ -0,0 +1,10 @@ +--TEST-- +"join" filter +--TEMPLATE-- +{{ ["foo", "bar"]|join(', ') }} +{{ foo|join(', ') }} +--DATA-- +return array('foo' => new Foo()) +--EXPECT-- +foo, bar +1, 2 \ No newline at end of file diff --git a/test/Twig/Tests/integrationTest.php b/test/Twig/Tests/integrationTest.php index bf8cc8fad..6d36d6dd0 100644 --- a/test/Twig/Tests/integrationTest.php +++ b/test/Twig/Tests/integrationTest.php @@ -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';