The @first data variable evaluates to true for the first element when iterating over an array or object with #each. Use it inside an {{#if @first}} block to conditionally render content only on the first iteration.
Syntax
{{#each field}}{{#if @first}} expr {{/if}}{{/each}}
Examples
Render the first key of an object
{{#each @root.child}}
{{#if @first}}{{@key}}{{/if}}
{{/each}}
Input:
{
"child": {
"childTitle": "Child Title",
"childBody": "Child Body"
}
}
Output: childTitle
@first is true for childTitle, the first key in the child object. childBody is skipped.
Render the index of the first array element
{{#each array}}
{{#if @first}}{{@key}}{{/if}}
{{/each}}
Input:
{ "array": [1, 2, 3, 4] }
Output: 0
Array iteration always starts at index 0. @first is true only for that first element regardless of the array's content or length.
Usage notes
-
@firstis only available inside a#eachblock. It has no meaning outside of iteration. - When iterating over an array,
@firstcorresponds to index0. - When iterating over an object,
@firstcorresponds to the first key in the object. - To access
@firstfrom a parent#eachloop inside a nested#eachblock, use{{@../first}}. -
@lastis the companion variable for the final iteration. It is only available when iterating over arrays, not objects.