Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can mustache iterate a top-level array?

My object looks like this:

['foo','bar','baz'] 

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

"<ul><li>foo</li><li>bar</li><li>baz</li></ul>" 

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

{list:['foo','bar','baz']} 
like image 284
greim Avatar asked Jun 29 '11 05:06

greim


1 Answers

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'}]; var tmp = '<ul>{{#.}}<li>{{name}}</li>{{/.}}</ul>'; Mustache.render(tmp, obj); 
like image 130
Dan Jordan Avatar answered Sep 29 '22 03:09

Dan Jordan