You are here

function twig_slice in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/twig/twig/lib/Twig/Extension/Core.php \twig_slice()

Slices a variable.

Parameters

Twig_Environment $env A Twig_Environment instance:

mixed $item A variable:

int $start Start of the slice:

int $length Size of the slice:

bool $preserveKeys Whether to preserve key or not (when the input is an array):

Return value

mixed The sliced variable

2 calls to twig_slice()
twig_first in vendor/twig/twig/lib/Twig/Extension/Core.php
Returns the first element of the item.
twig_last in vendor/twig/twig/lib/Twig/Extension/Core.php
Returns the last element of the item.
1 string reference to 'twig_slice'
Twig_Extension_Core::getFilters in vendor/twig/twig/lib/Twig/Extension/Core.php

File

vendor/twig/twig/lib/Twig/Extension/Core.php, line 715

Code

function twig_slice(Twig_Environment $env, $item, $start, $length = null, $preserveKeys = false) {
  if ($item instanceof Traversable) {
    if ($item instanceof IteratorAggregate) {
      $item = $item
        ->getIterator();
    }
    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) {
        return array();
      }
    }
    $item = iterator_to_array($item, $preserveKeys);
  }
  if (is_array($item)) {
    return array_slice($item, $start, $length, $preserveKeys);
  }
  $item = (string) $item;
  if (function_exists('mb_get_info') && null !== ($charset = $env
    ->getCharset())) {
    return (string) mb_substr($item, $start, null === $length ? mb_strlen($item, $charset) - $start : $length, $charset);
  }
  return (string) (null === $length ? substr($item, $start) : substr($item, $start, $length));
}