0

我遇到了一个奇怪的情况,我的模板没有渲染每个字段

非常感谢

'<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>'
4

2 回答 2

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您可以在路由 (in )中添加另一个返回的数据值client/helpers/router.js,使其看起来像这样:

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

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

<h2>These are {{photographer}}'s stories</h2>
于 2014-01-04T10:00:31.910 回答
0

在未显示的对象上调用整个模板。让所有人都称这个对象为顶部。我假设它看起来像这样:

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

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

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

Template.photog.photographer = function()
{
    return "the name";
}
于 2014-01-04T03:53:46.147 回答