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?
This is how I do it in JavaScript:
var idx = 0; var data = { "names": [ {"name":"John"}, {"name":"Mary"} ], "idx": function() { return idx++; } }; var html = Mustache.render(template, data);
Your template:
{{#names}} {{name}} is {{idx}} {{/names}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With