Take the 2-minute tour ×

My object looks like this:


And I want to use a mustache template to produce from it something like this:


But how? Do I really have to munge it into something like this first?

share|improve this question
you should probably change your accepted answer –  frank blizzard Aug 20 '12 at 10:41

4 Answers 4

up vote 90 down vote accepted

You can do it like this...

Mustache.render('<ul>{{#.}}<li>{{.}}</li>{{/.}}</ul>', ['foo','bar','baz']);

It also works for things like this...

var obj = [{name: 'foo'}, {name: 'bar'}];
Mustache.render(tmp, obj);
share|improve this answer
actually the template comes first: Mustache.render('<ul>{{#.}}<li>{{.}}</li>{{/.}}</ul>',['foo','bar','baz']); –  Kai Carver May 4 '12 at 12:06

I had the same problem this morning and after a little experimentation I discovered you can use the {{.}} to refer to the current element of an array:

share|improve this answer
Where does the name of the #yourList variable come from? can you show a javascript sample of the actual rendering? –  iwein Apr 15 '12 at 11:43
you don't even need to use "yourList", you can just use "." here too: Mustache.render('<ul>{{#.}}<li>{{.}}</li>{{/.}}</ul>',['foo','bar','baz']); –  Kai Carver May 5 '12 at 11:34
The JavaScript would be Mustache.render('<ul>{{#.}}{{.}}{{/.}}</ul>', {yourList: ['foo','bar','baz']}); –  danjordan May 22 '12 at 15:54
Note, only do this if you want less-readable templates. The accepted answer, albeit not "required" is a more readable solution. –  timoxley May 29 '12 at 12:01
Does anyone know why it is that this information is absent from the documentation? mustache.github.io/mustache.5.html –  Josh Jun 25 at 19:01

Building on @danjordan's answer, this will do what you want:



share|improve this answer

I don't think mustache can do this! (surprisingly) You can iterate over a list of objects, and then access the attributes of each object, but you can't seem to iterate over a simple list of values!

So, you have to transform your list into:

[ {"value":"foo"},{"value":"bar"},{"value":"baz"} ] 

and then your template would be:


To me, this seems like a severe problem with Mustache -- any template system should be able to loop over a list of simple values!

share|improve this answer
You just need to use {{.}}. See my answer below. –  Andy Hull Dec 2 '11 at 17:50

Your Answer


