The .length property returns the number of characters in a string or the number of elements in an array. Access it via dot-notation on any field (field.length) and use it as an argument inside other helpers such as #compare.
Syntax
{{field.length}}
Or as an argument inside another helper:
{{#compare field.length operator value}} expr {{else}} expr {{/compare}}
Examples
Validate a state code length
{{#compare state.length "===" 2}}{{uppercase state}}{{else}}Bad state value{{/compare}}
When state is "ill" (3 characters):
Input:
{ "state": "ill" }
Output: Bad state value
When state is "il" (2 characters):
Input:
{ "state": "il" }
Output: IL
The #compare helper checks whether state.length equals 2. If it does, {{uppercase state}} renders the value in capitals. If not, the fallback text renders.
Validate a phone number length
{{#compare phone.length "===" 10}}{{phone}}{{else}}0000000000{{/compare}}
Input:
{ "phone": "123456789" }
Output: 0000000000
"123456789" has 9 characters, not 10, so the {{else}} branch renders.
Usage notes
-
.lengthis accessed via dot-notation directly on a field, not as a standalone helper or@variable. - For strings,
.lengthcounts characters. For arrays,.lengthcounts elements. -
.lengthreturns0for empty strings and empty arrays. - Use
.lengthas an argument inside#compareto validate field lengths before processing.