The modulo helper returns the remainder when dividing the first number (a) by the second (b). This is useful for performing arithmetic operations or creating repeating patterns in templates.
{{modulo a b}}
-
a (required): The dividend (number to divide)
-
b (required): The divisor (number to divide by)
Basic remainder calculation
{{modulo record.a record.b}}
Input:
{
"record": {
"a": 7,
"b": 3
}
}
Output:
1
Determine even or odd values
{{#if (eq (modulo record.value 2) 0)}}
Even
{{else}}
Odd
{{/if}}
Input:
{ "record": { "value": 9 } }
Output:
Odd
Use in a loop to create row groups
{{#each record.items}}
{{#if (eq (modulo @index 3) 0)}}<hr>{{/if}}
{{this}}
{{/each}}
Input:
{
"record": {
"items": ["A", "B", "C", "D", "E", "F"]
}
}
Output:
A B C <hr> D E F
Tips
Tip
-
The helper performs arithmetic using JavaScript’s
%operator. -
Works best for grouping, alternating layouts, or computing sequence offsets.