Search code examples
mustache

Do mustache.js templates modulas operator?


Is there a modulas operator (%) for mustache templates?

The modulas operator helps to determine if the current record being processed is even or odd.


Solution

  • No. Mustache templates generally do not have any operator of any kind, nor they have any syntax for number literals: {{ index % 2 }} fails for both the % operator, and the 2 literal. The only expressions allowed in a Mustache tag are identifiers such as {{ foo }} and {{ foo.bar }}.

    Now, you can look after Handlebars.js templates. They are compatible with Mustache, and are much more versatile and flexible. Finding odd and even indexes is quite possible with Handlebars. You won't have any % operator, but you'll have rich "helpers" that will allow you to write something similar to {{# even @index }}...{{ else }}...{{/ even }}