Handlebars Helper Function: slice

Returns a portion of a list between two specified indices, similar to JavaScript's Array.slice().

Availability

This helper is available in the Winter ’26 release of Marketing Cloud Next.

Syntax

{{slice list startIndex endIndex}}

Parameters

Parameter Type Description
list Array Required. The list to slice.
startIndex Integer Required. The starting index. Use negative indices to count from the end.
endIndex Integer The ending index. Use negative indices to count from the end. If you don’t specify an end index, the function returns a sublist from the startIndex to the end of the list.

Return Value

The function returns a sublist from startIndex to endIndex, not including the element at the endIndex. List indices start at 0. If indices are out of bounds or invalid, the function throws an exception. If the array is empty, the function returns an empty list.

Usage Examples

This example shows how to use the slice function to return a sublist.

myList = ["apple", "banana", "cherry", "date", "elderberry"];

{{slice myList 1 3}}

The function returns ["banana", "cherry"].

Slice from the startIndex to the End of the List

If you don't specify an end index, the function returns a sublist from the startIndex to the end of the list, as shown in this example.

myList = ["apple", "banana", "cherry", "date", "elderberry"];

{{slice myList 2}}

The function returns ["cherry", "date", "elderberry"].

Index from the End of the List

You can also use negative indices to index the list from the end. Negative indices start at -1. The element at the endIndex isn’t included in the sublist, regardless of whether it’s positive or negative.

This example shows how to use negative indices to index the list from the end.

myList = ["apple", "banana", "cherry", "date", "elderberry"];

{{slice myList -2 -1}}

The function returns ["date"].

Combine Positive and Negative Indices

You can also specify a positive startIndex and a negative endIndex. This example shows how to return a list that includes the second through the second-to-last elements.

myList = ["apple", "banana", "cherry", "date", "elderberry"];

{{slice myList 1 -1}}

The function returns ["banana", "cherry", "date"].