如何遍历 Handlebars 中的对象数组? [英] 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.
我正在使用这个以 JSON 格式返回对象数组的 Web API:
I'm hitting this Web API that returns an array of objects in JSON format:
Handlebars 文档显示了以下示例:
Handlebars docs shows the following example:
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>
在上下文中:
{
people: [
"Yehuda Katz",
"Alan Johnson",
"Charles Jolley"
]
}
就我而言,我没有数组的名称,它只是响应的根对象.我试过使用 {{#each}}
没有运气.
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.
第一次使用 Handlebars...我错过了什么?
First time using Handlebars... What am I missing?
更新
这是一个简化的小提琴,向您展示我的要求:http://jsfiddle.net/KPCh4/2/
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?
推荐答案
您可以将 this
传递给每个块.请参阅此处:http://jsfiddle.net/yR7TZ/1/
You can pass this
to each block. See here: http://jsfiddle.net/yR7TZ/1/
{{#each this}}
<div class="row"></div>
{{/each}}
这篇关于如何遍历 Handlebars 中的对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!