循环通过json传递组合部分变量

时间:2014-08-14 20:40:21

标签: json assemble

我在使用以下结构中的汇编站点设置尝试循环我的JSON数据时遇到了麻烦:

-src
--content
---index.hbs
--data
---steps-data.json
--partial
---steps.hbs

内容中的index.hbs调用部分传递对象,如下所示:

{{> steps steps-data}}

我的steps-data.json文件如下所示:

{
    "steps":[{
        "index": 1,
        "title": "Find a product",
        "description": "Go to a product page and click on the +PriceTrack short cut to add to your list."
    },{
        "index": 2,
        "title": "Track its price",
        "description": "Go to a product page and click on the +PriceTrack short cut to add to your list."
    },{
        "index": 3,
        "title": "Purchase",
        "description": "Go to a product page and click on the +PriceTrack short cut to add to your list."
    }]
}

在我的steps.hbs中,我尝试循环遍历JSON数据但不是。

<div class="g-col g-span4">
    {{#each steps-data.steps}}
        <div class="working-step">
            <span class="number"></span><h3>{{title}}</h3>
        </div>
        <p>{{description}}</p>
    {{/each}}
</div>

我遇到的问题是它没有循环,也不确定我做错了什么。

1 个答案:

答案 0 :(得分:2)

由于您已将steps-data传递给部分内容,因此您可以直接或steps访问this.steps

<div class="g-col g-span4">
    {{#each this.steps}}
        <div class="working-step">
            <span class="number"></span><h3>{{title}}</h3>
        </div>
        <p>{{description}}</p>
    {{/each}}
</div>