You can cut and display a part of an array or string with the slice filter. If it is an array, foreach is used with it. Its first parameter defines from which element it starts (note that the very first element is signed by 0), and the second parameter defines the number of elements to cut (note that it includes the starting element as well). In the next example, the starting element of an array is 1, so it will start from the second element, and the number of elements is 2, so it will cut 2 elements starting with the second element.
{% foreach item in [1, 2, 3, 4, 5]|slice(1, 2) %}
{{ item }} <br />
{% endforeach %}
Result
2
3
An example for slicing a string is the following:
{{ '12345'|slice(2, 2) }}
Result
34
If the starting number is negative, counting starts from the end of the array or string (e.g. -4 will sign the second element, which is 2 in the above example). If no second parameter is provided, it means including all upcoming elements. Please note that zero cannot be a second parameter.
{{ '12345'|slice(-4) }}
Result
2345