为什么Spacebars模板没有完全呈现?

时间:2014-01-04 02:40:40

标签: templates meteor spacebars

我有一个奇怪的情况,我的模板没有呈现每个字段

非常感谢

'<template name="photog">
<h2>These are {{photographer}}'s stories</h2>

{{#each photog}}

        <img src="{{photos.[0]}}" alt="great image from a story">

        <div class="caption">

            <h3>{{storyName}}</h3>

            <p>Photographer: {{photographer}}</p>

            <p>Editor: <a href="/editor/{{editor}}">{{editor}}</a></p>

        </div>
{{/each}}
</template>'

2 个答案:

答案 0 :(得分:1)

在模板的{{#each}}部分之外无法使用摄影师的名字,因为从photog路线查询返回的数据没有顶级摄影师字段:

{
    "photographer" : "Dean",
    "editor" : "Dean",
    "votes" : 0,
    "photos" : [
        "/pics/wenick_20131110_171.jpg",
        "/pics/wenick_20131110_182.jpg"
    ],
    "storyName" : "D2BS West 2013",
    "_id" : "GCYR9MnYrrvxRSBQB"
}
{
    "photographer" : "Dean",
    "editor" : "Dean",
    "votes" : 1,
    "photos" : [
        "/pics/wenick_20130409_149.jpg",
        "/pics/wenick_20130409_158.jpg"
    ],
    "storyName" : "Bill's Party",
    "_id" : "tCrFAm7X6vFSbiadC"
}

您可以在photog路线(client/helpers/router.js)中添加其他返回的数据值,使其如下所示:

return {
            photog: Stories.find( {photographer: this.params.photographer} ),
            photographer: this.params.photographer
        };

...然后确保引用摄影师姓名的行(client/views/stories/photog.html)为:

<h2>These are {{photographer}}'s stories</h2>

答案 1 :(得分:0)

在未显示的对象上调用整个模板。让我们把这个对象称为top。我认为它看起来像这样:

{
    "photog": [
        {
            "photographer": "name",
            "editor": "name",
            "photos": []
        },
        {
            "photographer": "name2",
            "editor": "name2",
            "photos": []
        }
    ]
}

{{#each}}循环通过被调用的photog数组。在每个变量范围内都进行了更改,以便您可以直接访问{{photographer}}。

但是在每个{{photographer}}模板引用的范围之外?它不知道。一种解决方案是使用模板助手硬连接。将这样的内容添加到您的javascript:

Template.photog.photographer = function()
{
    return "the name";
}