1

嗨,我是小胡子的新手,最近一直在玩它。

我喜欢它的简单性以及它只专注于演示的方式。没有逻辑!

在 node.js 中使用 mongodb 和 mongoose 时,我要求一个名为 products 的集合。目前我收到的所有文件都非常小。

显示此数据时,我想使用小胡子的列表功能。

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

我正在使用的 BSON 响应是:

[
  {
    "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"
  }
 ]

结果是一个数组,mustache 在其模板中需要一个数组名称吗?是否可以使用正确的语法将 BSON 直接打入 mustache 模板?

我可以在 mongo 的响应中添加一个数组名称吗?甚至通过节点。{{arrayname}} & {{/arrayname}}

谢谢。

4

1 回答 1

1

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:

bobjim

Are you having problems with this syntax? Which Mustache variant are you using. The latest version is here: https://github.com/janl/mustache.js

于 2013-02-19T22:16:53.890 回答