This is what I’d like to do in Mustache.js but not seeing how with the documentation.
var view = {items:['Mercury','Venus','Earth','Mars']};
var template = "<ul> {{#items}}<li>{{i}} - {{.}}</li>{{/items}} </ul>";
var html = Mustache.to_html(template,view);
Desired output:
<ul>
<li>0 - Mercury</li>
<li>1 - Venus</li>
<li>2 - Earth</li>
<li>3 - Mars</li>
</ul>
An alternative solution, without fooling around with Mustache.js
Instead of fooling around with mustache you might as well use a
<ol></ol>instead of<ul></ul>, that will prefix each item withindex+1.If you’d like you can use css to change the starting number to 0, and it will render exactly as you want. You can even change the
dotafter the number, to something such as" - ", and problem is solved.the above will render as:
the Mustache.js way to do it
The proper method if you’d like to do it in mustache is to convert your array of strings to an array of objects, where index and string value is present.