/**
* Slices a variable.
*
- * @param Twig_Environment $env A Twig_Environment instance
- * @param mixed $item A variable
- * @param integer $start Start of the slice
- * @param integer $length Size of the slice
+ * @param Twig_Environment $env A Twig_Environment instance
+ * @param mixed $item A variable
+ * @param integer $start Start of the slice
+ * @param integer $length Size of the slice
+ * @param Boolean $preserveKeys Whether to preserve key or not (when the input is an array)
*
* @return mixed The sliced variable
*/
-function twig_slice(Twig_Environment $env, $item, $start, $length = null)
+function twig_slice(Twig_Environment $env, $item, $start, $length = null, $preserveKeys = false)
{
if ($item instanceof Traversable) {
$item = iterator_to_array($item, false);
}
if (is_array($item)) {
- return array_slice($item, $start, $length);
+ return array_slice($item, $start, $length, $preserveKeys);
}
$item = (string) $item;
{{ {a: 1, b: 2, c: 3, d: 4}[1:2]|join('') }}
{{ [1, 2, 3, 4][start:length]|join('') }}
{{ [1, 2, 3, 4]|slice(1, 2)|join('') }}
+{{ [1, 2, 3, 4]|slice(1, 2)|keys|join('') }}
+{{ [1, 2, 3, 4]|slice(1, 2, true)|keys|join('') }}
{{ {a: 1, b: 2, c: 3, d: 4}|slice(1, 2)|join('') }}
+{{ {a: 1, b: 2, c: 3, d: 4}|slice(1, 2)|keys|join('') }}
{{ '1234'|slice(1, 2) }}
{{ '1234'[1:2] }}
{{ arr|slice(1, 2)|join('') }}
23
23
23
+01
+12
23
+bc
23
23
23