Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

loops - How to iterate over array of objects in Handlebars?

This might seem a silly question but I can't seem to find the answer anywhere.

I'm hitting this Web API that returns an array of objects in JSON format:

array of objects

Handlebars docs shows the following example:

<ul class="people_list">
  {{#each people}}
  <li>{{this}}</li>
  {{/each}}
</ul>

In the context of:

{
  people: [
    "Yehuda Katz",
    "Alan Johnson",
    "Charles Jolley"
  ]
}

In my case I don't have a name for the array, it's just the root object of the response. I've tried using {{#each}} with no luck.

First time using Handlebars... What am I missing?

UPDATE

Here's a simplified fiddle to show you what I'm asking: http://jsfiddle.net/KPCh4/2/

Does handlebars require the context variable to be an object and not an array?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can pass this to each block. See here: http://jsfiddle.net/yR7TZ/1/

{{#each this}}
    <div class="row"></div>
{{/each}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...