如何读取 Handlebars 对象数组中的属性?

How to read properties in an array of objects in Handlebars?

我正在制作一个带有记分牌的寻宝应用程序。在 scoreboard 路由中,我异步调用 return JSON 来自 MongoDB 数据的函数。这就是函数 returns:

[
  {
    "_id": "rational",
    "total": 0
  },
  {
    "_id": "creative",
    "total": 0
  },
  {
    "_id": "confident",
    "total": 60
  },
  {
    "_id": "passionate",
    "total": 30
  },
  {
    "_id": "ingenious",
    "total": 30
  }
]

_id是球队名称,total是球队得分。我正在使用 Handlebars 作为我的模板引擎,但对它还是个新手。我想制作一个 HTML table 来显示球队名称和各自的总分,但似乎无法弄清楚。请帮我解决这个问题。

说实话,你可以通过阅读 the fine manual 来解决这个问题,但这里有一个例子:

<table>
  {{#each teams}}
  <tr>
    <td>{{_id}}</td>
    <td>{{total}}</td>
  </tr>
  {{/each}}
</table>

在您的处理程序中,您呈现类似于这样的模板:

res.render('teams.hbs', { teams : [ LIST OF TEAMS ] });