mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 03:46:39 +00:00
Fix for broken BC through #1503: The slice filter works as the array_slice PHP function for arrays
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user