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_ function.
String keys are always preserved, numeric keys only if preserve
is set.
For strings, it mimicks PHP's mb_ 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_
function.
Examples
Given items = .
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
-
- 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