diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 92feea5e7..346006d32 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -707,7 +707,7 @@ function twig_slice(Twig_Environment $env, $item, $start, $length = null, $prese $item = $item->getIterator(); } - if ($start >= 0 && $length >= 0) { + if ($start >= 0 && $length >= 0 && $item instanceof Iterator) { try { return iterator_to_array(new LimitIterator($item, $start, $length === null ? -1 : $length), $preserveKeys); } catch (OutOfBoundsException $exception) { diff --git a/test/Twig/Tests/Fixtures/filters/slice.test b/test/Twig/Tests/Fixtures/filters/slice.test index c10fe9355..b49b89fe5 100644 --- a/test/Twig/Tests/Fixtures/filters/slice.test +++ b/test/Twig/Tests/Fixtures/filters/slice.test @@ -24,8 +24,9 @@ {{ arr|slice(3)|join('') }} {{ arr[2:]|join('') }} +{{ xml|slice(1)|join('')}} --DATA-- -return array('start' => 1, 'length' => 2, 'arr' => new ArrayObject(array(1, 2, 3, 4))) +return array('start' => 1, 'length' => 2, 'arr' => new ArrayObject(array(1, 2, 3, 4)), 'xml' => new SimpleXMLElement('12')) --EXPECT-- 23 23 @@ -49,4 +50,5 @@ bc 1 4 -34 \ No newline at end of file +34 +2