Aamir Ahmad
Aamir Ahmad

Reputation: 15

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

I am making a Treasure Hunt app which has a scoreboard. In the scoreboard route I am asynchronously calling a function which return JSON data from MongoDB. This is what the function returns:

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

The _id is the team name and total is the number of points the team scored. I am using Handlebars as my templating engine and am still new to it. I want to make a HTML table that shows the team names with respective total scores but can't seem to figure it out. Please help me solve this problem.

Upvotes: 1

Views: 605

Answers (1)

robertklep
robertklep

Reputation: 203251

To be honest, you could have solved this by reading the fine manual, but here's an example:

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

In your handler, you render the template similar to this:

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

Upvotes: 3

Related Questions