115

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']}
0

6 Answers 6

184

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);
6
  • 2
    actually the template comes first: Mustache.render('<ul>{{#.}}<li>{{.}}</li>{{/.}}</ul>',['foo','bar','baz']);
    – Kai Carver
    May 4, 2012 at 12:06
  • How do I get, for example, the 2nd element of the array? I'm trying to do {{.1}} with mustache.js and its not working.
    – dessalines
    Apr 2, 2015 at 17:22
  • NM, figured it out: you can just ignore the dots: so {{1}} or if you want to do a logical check, then {{#1}} whatever {{/1}}
    – dessalines
    Apr 2, 2015 at 17:29
  • 8
    Are these features documented somewhere? I don't see {{.}}, {{1}}, or anything similar in mustache(5). Jul 21, 2015 at 23:42
  • Note: top level array is not supported by Hogan: github.com/twitter/hogan.js/issues/74. Use the solution with a property: stackoverflow.com/a/8360440/470117
    – mems
    Apr 4, 2019 at 20:11
134

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:

<ul>
  {{#yourList}}
  <li>{{.}}</li>
  {{/yourList}}
</ul>
10
  • 1
    Where does the name of the #yourList variable come from? can you show a javascript sample of the actual rendering?
    – iwein
    Apr 15, 2012 at 11:43
  • 4
    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, 2012 at 11:34
  • The JavaScript would be Mustache.render('<ul>{{#.}}{{.}}{{/.}}</ul>', {yourList: ['foo','bar','baz']});
    – Dan Jordan
    May 22, 2012 at 15:54
  • 1
    Note, only do this if you want less-readable templates. The accepted answer, albeit not "required" is a more readable solution.
    – timoxley
    May 29, 2012 at 12:01
  • 8
    Does anyone know why it is that this information is absent from the documentation? mustache.github.io/mustache.5.html
    – Josh
    Jun 25, 2014 at 19:01
5

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

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

returning:

<ul><li>foo</li><li>bar</li><li>baz</li></ul>
1
  • It works only for array's not for objcts, impossible for {a:'foo',b:'bar',c:'baz'}... How to do anonymous references when iterationg over objects? Jun 14, 2018 at 20:08
3

Following are the examples to render multi-dimensional array in a template:

Example 1

'use strict';

var Mustache = require('mustache');

var view = {test: 'div content', multiple : ['foo', 'bar'], multiple_2 : ['hello', 'world']};
var template = '<div>{{test}}</div><ul>{{#multiple}}<li>{{.}}</li>{{/multiple}}</ul><ul>{{#multiple_2}}<li>{{.}}</li>{{/multiple_2}}</ul>';

var output = Mustache.render(template, view);

console.log(output);

Example 2

'use strict';

var Mustache = require('mustache');

var view = {test: 'div content', multiple : [{name: 'foo', gender: 'male'}, {name: 'bar', gender: 'female'}], multiple_2 : [{text: 'Hello', append: '**', prepend: '**'}, {text: 'World', append: '**', prepend: '**'}]};
var template = '<div>{{test}}</div><ul>{{#multiple}}<li>Hello my name is {{name}}. And I am {{gender}}</li>{{/multiple}}</ul><ul>{{#multiple_2}}<li>{{prepend}}_{{text}}_{{append}}</li>{{/multiple_2}}</ul>';

var output = Mustache.render(template, view);

console.log(output);

For test run, save above examples in file called 'test.js', run following command in commandline

nodejs test.js
0

Simplest solution for an array named Strengths:

{
   "ViewModel": 
    {
       "StrengthsItems": 
        {
           "Strengths": ["Dedicated", "Resourceful", "Professional", "Positive"]
        }
    }
}

works for me like a charm is as following:

{{#ViewModel.StrengthsItems}}
    <p class="p4">{{Strengths}}</p>
{{/ViewModel.StrengthsItems}}
-2

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:

<ul>
  {{#the_list}}
  <li>{{value}}</li>
  {{/the_list}}
</ul>

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

3
  • 4
    You just need to use {{.}}. See my answer below.
    – Andy Hull
    Dec 2, 2011 at 17:50
  • 2
    The down-votes are misleading. This answer is correct in that {{.}} is not part of the mustache standard, though it is supported by some implementations. There is no portable way to do this.
    – Yefu
    Feb 7, 2017 at 0:57
  • this one is right, and very usefully for multidimensional rendering. Please find my example as below Nov 8, 2017 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.