Slice ViewHelper <f: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 

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

First items of an array 

<f:slice value="{items}" length="3" />
Copied!
['a', 'b', 'c']
Copied!

Skip the first item 

<f:slice value="{items}" offset="1" />
Copied!
['b', 'c', 'd', 'e']
Copied!

Last items of an array 

<f:slice value="{items}" offset="-2" />
Copied!
['d', 'e']
Copied!

Render only the first items 

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

Copied!

Part of a string 

<f:slice value="Hello World" offset="6" />
Copied!
World
Copied!

Go to the source code of this ViewHelper: SliceViewHelper.php (GitHub).

Arguments

The following arguments are available for the slice ViewHelper:

length

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

offset
Type
integer
Default
0
Position of the first item or character. If negative, the part starts that far from the end.

preserveKeys

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

value
Type
mixed
The array or string to extract a part from