Articles in this section

hasNoItems helper

The hasNoItems helper returns true when an array has no elements or an object has no keys. Use it inside #if or #unless blocks to conditionally render content based on whether a collection is empty.

Syntax

{{hasNoItems value}}

value -- The array or object to evaluate. Required.

Examples

Check arrays and objects for emptiness

{{hasNoItems record.arr1}}
{{hasNoItems record.arr2}}
{{hasNoItems record.obj1}}
{{hasNoItems record.obj2}}

Input:

{
 "record": {
 "arr1": [],
 "arr2": [1, 2],
 "obj1": {},
 "obj2": { "x": 1 }
 }
}

Output:

true
false
true
false

arr1 and obj1 are empty, so those return true. arr2 has two elements and obj2 has one key, so those return false.

Use in a conditional block

{{#if (hasNoItems record.items)}}
 No items found
{{else}}
 Items present
{{/if}}

Input:

{ "record": { "items": [] } }

Output: No items found

Wrap hasNoItems in parentheses to use it as an inline expression inside #if.

Usage notes

  • Works with both arrays and plain objects.
  • Returns true only when there are no elements (arrays) or no keys (objects) -- a single element or key returns false.
  • Wrap hasNoItems in parentheses when using it as an inline expression: {{#if (hasNoItems value)}}.
  • To also handle null and undefined values, use the #isEmpty helper instead -- hasNoItems is designed for collections that are present but empty.