Fix for broken BC through #1503: The slice filter works as the array_slice PHP function for arrays

This commit is contained in:
Roman Lasinski
2014-10-21 08:45:27 +02:00
parent 27d1e21064
commit d95d0fd300
2 changed files with 9 additions and 1 deletions
+5 -1
View File
@@ -708,7 +708,11 @@ function twig_slice(Twig_Environment $env, $item, $start, $length = null, $prese
}
if ($start >= 0 && $length >= 0) {
return iterator_to_array(new LimitIterator($item, $start, $length === null ? -1 : $length), $preserveKeys);
try {
return iterator_to_array(new LimitIterator($item, $start, $length === null ? -1 : $length), $preserveKeys);
} catch (\OutOfBoundsException $exception) {
return array();
}
}
$item = iterator_to_array($item, $preserveKeys);
@@ -13,6 +13,8 @@
{{ '1234'[1:2] }}
{{ arr|slice(1, 2)|join('') }}
{{ arr[1:2]|join('') }}
{{ arr[4:1]|join('') }}
{{ arr[3:2]|join('') }}
{{ [1, 2, 3, 4]|slice(1)|join('') }}
{{ [1, 2, 3, 4][1:]|join('') }}
@@ -38,6 +40,8 @@ bc
23
23
4
234
234
234