Articles in this section

@last data variable

The @last data variable evaluates to true for the final element when iterating over an array with #each. Use it inside an {{#if @last}} block to conditionally render content only on the last iteration.

Syntax

{{#each field}}{{#if @last}} expr {{/if}}{{/each}}

Examples

Render the last key of an object

{{#each @root.child}}
  {{#if @last}}{{@key}}{{/if}}
{{/each}}

Input:

{
  "child": {
    "childTitle": "Child Title",
    "childBody": "Child Body"
  }
}

Output: childBody

@last is true for childBody, the final key in the child object. childTitle is skipped.

Render the index of the last array element

{{#each array}}
  {{#if @last}}{{@key}}{{/if}}
{{/each}}

Input:

{ "array": [1, 2, 3, 4] }

Output: 3

The array has four elements, so the last index is 3. @last returns the same index value regardless of the array's content type — a four-element array of strings also returns 3.

Usage notes

  • @last is only available inside a #each block. It has no meaning outside of iteration.
  • @last is available when iterating over both arrays and objects.
  • The last index of an array is always the array's length minus one (zero-based).
  • To access @last from a parent #each loop inside a nested #each block, use {{@../last}}.
  • @first is the companion variable for the first iteration. Unlike @last, @first is available for both arrays and objects.