Moustache.js和来自mongodb的bson数据中的列表

Lists in Moustache.js and bson data from mongodb

本文关键字:bson 数据 列表 mongodb js Moustache      更新时间:2023-09-26

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

我喜欢它的简洁性以及它如何专注于演示。没有逻辑!

在node.js中使用mongodb和mongose时,我要求一个名为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"
  }
 ]

结果是一个数组,小胡子希望在其模板中为数组命名?是否可以用正确的语法将BSON直接打到胡子模板中?

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

谢谢。

您几乎做到了。假设data=您提供的BSON数据段,template=您提供的模板,这应该有效:

Mustache.render(template, data)

例如,一个简化的例子如下:

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

将输出:

bobjim

你的语法有问题吗?您使用的是哪种Mustache变体。最新版本如下:https://github.com/janl/mustache.js