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
-
@lastis only available inside a#eachblock. It has no meaning outside of iteration. -
@lastis 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
@lastfrom a parent#eachloop inside a nested#eachblock, use{{@../last}}. -
@firstis the companion variable for the first iteration. Unlike@last,@firstis available for both arrays and objects.