From 19ba9c2141f3edcffd3c20be07b61abd0a018c31 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 2 Jan 2012 13:10:08 +0100 Subject: [PATCH] added Traversable objects support for the join filter --- CHANGELOG | 4 ++++ lib/Twig/Extension/Core.php | 8 ++++++++ test/Twig/Tests/Fixtures/filters/join.test | 10 ++++++++++ test/Twig/Tests/integrationTest.php | 7 ++++++- 4 files changed, 28 insertions(+), 1 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/join.test diff --git a/CHANGELOG b/CHANGELOG index d4e1e69..360eaf7 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 5083412..a042c37 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 0000000..244fa10 --- /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 bf8cc8f..6d36d6d 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'; -- 1.7.2.5