---
title: "Slice ViewHelper <f:slice>"
manual: "Fluid Explained"
version: "main"
permalink: "https://docs.typo3.org/permalink/fluid:typo3fluid-fluid-slice"
source: "ViewHelpers/Fluid/Slice.rst"
rendered: "2026-11-16T10:01:33+00:00"
---

# Slice ViewHelper `<f:slice>` {#typo3fluid-fluid-slice}

The SliceViewHelper extracts a part of an array or a string, defined by
an offset and a length.

For arrays, this ViewHelper mimicks PHP's `array_slice()` function.
String keys are always preserved, numeric keys only if `preserveKeys`
is set.

For strings, it mimicks PHP's `mb_substr()` function, so offset and
length count characters, not bytes.

Iterators like generators can yield the same key more than once. Only the
last item per key is used, like with PHP's `iterator_to_array()`
function.

## Examples {#examples}

Given `items = ['a', 'b', 'c', 'd', 'e']`.

### First items of an array {#first-items-of-an-array}

```php
<f:slice value="{items}" length="3" />

```

```php
['a', 'b', 'c']
```

### Skip the first item {#skip-the-first-item}

```php
<f:slice value="{items}" offset="1" />

```

```php
['b', 'c', 'd', 'e']
```

### Last items of an array {#last-items-of-an-array}

```php
<f:slice value="{items}" offset="-2" />

```

```php
['d', 'e']
```

### Render only the first items {#render-only-the-first-items}

```php
<f:for each="{items -> f:slice(length: 3)}" as="item">
    <span>{item}</span>
</f:for>

```

### Part of a string {#part-of-a-string}

```php
<f:slice value="Hello World" offset="6" />

```

```text
World
```

Go to the source code of this ViewHelper: [SliceViewHelper.php (GitHub)](https://github.com/TYPO3/Fluid/blob/main/src/ViewHelpers/SliceViewHelper.php).

**Arguments**

The following arguments are available for the slice ViewHelper:

-   **length**

    -   *Type:* integer

    Maximum number of items or characters. If negative, the part stops that far from the end. If omitted, everything from the offset on is returned.

-   **offset**

    -   *Type:* integer
    -   *Default:* 0

    Position of the first item or character. If negative, the part starts that far from the end.

-   **preserveKeys**

    -   *Type:* boolean
    -   *Default:* false

    If true, numeric keys of an array are preserved. String keys are always preserved. Has no effect on strings.

-   **value**

    -   *Type:* mixed

    The array or string to extract a part from
