templatesmustache

In Mustache, How to get the index of the current Section


I am using Mustache and using the data

{ "names": [ {"name":"John"}, {"name":"Mary"} ] }

My mustache template is:

{{#names}}
    {{name}}
{{/names}}

What I want to be able to do is to get an index of the current number in the array. Something like:

{{#names}}
    {{name}} is {{index}}
{{/names}}

and have it print out

John is 1
Mary is 2

Is it possible to get this with Mustache? or with Handlebars or another extension?


Solution

  • For reference, this functionality is now built in to Handlebars which has compatibility with Mustache.

    Use {{@index}}

    {{#names}}
        {{name}} is {{@index}}
    {{/names}}
    

    John is 0

    Mary is 1