Lists in Moustache.js and bson data from mongodb


Hi i'm new to moustache and have recently been playing around with it.

I like its simplicity and how it just focuses on presentation. no-logic!

When using mongodb and mongoose in node.js I am asking for a collection called products. I'm receiving all of the documents in the collection at the moment as its very small.

When displaying this data I would like to use moustache's list functionality.

<script id="listTemplate" type="text/html" >
<div class="singleItem">
    <div class="title">{{title}}</div>
    <div class="description">{{description}}</div>

The BSON response i'm using is:

    "title": "item",
    "description": "All about the details. Of course it's black.",
    "style": "12345",
    "_id": "5116874d2ac8dc2804000001",
    "__v": 0,
    "modified": "2013-02-09T17:28:45.000Z"
    "title": "item",
    "description": "All about the details. Of course it's black.",
    "style": "12348",
    "_id": "511688222ac8dc2804000003",
    "__v": 0,
    "modified": "2013-02-09T17:32:18.000Z"

The result is an array, moustache expects a name for arrays in its templates? Is it possible to punch BSON straight into the moustache template with the correct syntax?

Could I alternatively add an array name to the response in mongo? or even through node. {{arrayname}} & {{/arrayname}}


Problem courtesy of: mike james


You are pretty much there. Assuming that data = BSON data segment you provided and template = the template you provided, this should work:

Mustache.render(template, data)

For example, a simplified example of this would be the following:

console.log(Mustache.render('{{#.}}{{a}}{{/.}}', [{a:'bob'}, {a:'jim'}]));

That will output:


Are you having problems with this syntax? Which Mustache variant are you using. The latest version is here:

Solution courtesy of: syazdani


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.