Articles in this section

before helper

The before helper returns a new array excluding the last n elements of the input array. Use it to select only the first portion of an array or trim trailing elements before further processing.

Syntax

{{before array n}}

array -- The array to slice. Required. Must be an array -- before does not work on strings or other data structures.

n -- The number of elements to exclude from the end. Required. If n is greater than or equal to the array length, the helper returns an empty array.

Examples

Exclude the last two elements

{{before record.letters 2}}

Input:

{
 "record": {
 "letters": ["a", "b", "c", "d"]
 }
}

Output: ["a", "b"]

Keep only the first element

{{before record.items 3}}

Input:

{
 "record": {
 "items": ["apple", "banana", "cherry", "date"]
 }
}

Output: ["apple"]

Excludes the last three elements, leaving only the first.

Iterate over the remaining elements

{{#each (before record.tags 1)}}
 {{this}}
{{/each}}

Input:

{
 "record": {
 "tags": ["draft", "review", "published"]
 }
}

Output:

draft
review

Wrap before in a #each block to iterate directly over the returned array. Here, the last element ("published") is excluded.

Usage notes

  • before works only with arrays. For trimming strings, use a substring helper instead.
  • If n is greater than or equal to the array length, the helper returns an empty array.
  • before returns a new array and does not modify the original.
  • To skip elements from the beginning of an array instead, use the after helper.