0

我开始在我的 Meteor 应用程序中使用Iron Router及其yields模板。
我最近遇到了一个问题,我无法使用上下文启动命名收益,如下所示:

{{#with context}}
    {{yield 'subtemplate'}}
{{/with}}

并得到这个错误Sorry, couldn't find a yield named "subtemplate". Did you define it in one of the rendered templates like this: {{yield "subtemplate"}}?

如果我删除{{#with}}块表达式,我可以渲染产量。

有谁知道将上下文传递给命名收益的好方法?

我已将我的问题作为问题发布iron-routergithub 项目上,但尚未得到任何解决方案。

将不胜感激任何帮助。

编辑 1/1/2014:所以我的代码如下所示:

// main template for the route
 <div class="form-container">
    <div class="form">
        {{yield 'section'}}
    </div>
</div>

让产量部分显示的逻辑

// router.js
ApplyController = RouteController.extend({
    before: function() {
        var section = this.params.section || 'personal-info';
        Session.set('current', section);
    },
    action: function() {
        var section = Session.get('current');
        this.render();
        this.render(section, {
            to: 'section'
        });
    },
    data: function() {
        return {
            app: Applications.findOne({user: Meteor.userId()})
        }
    }
});

部分模板之一的示例:

<template name="education">
    {{#with app}}
    <form id="education" name="education" class="fragment" method="post" action="">
    <h2>Education</h2>
        <div class="form-group">
            <label for="college" class="control-label">College/ University</label>
            <select class="form-control" id="college" name="college" placeholder="Select a College/ University">
                <option value="">Select a College/ University</option>
            {{#each colleges}}
                <option value="{{slug}}" {{selected slug ../college}}>{{name}}</option>
            {{/each}}
            </select>
        </div>
        <!-- other content here -->
    </form>
    {{/with}}
</template>

使用{{#with app}}块是我目前解决这个问题的方法,但因为我有 10 个不同的部分模板,我必须把它放在所有模板中。

4

1 回答 1

1

您使用 Ironrouter 在路由器中传递数据上下文。您不能以这种方式传递它,因为如果您在路由器中传递一个路由,它将覆盖该路由的数据上下文。

然而,它可能适用于基于 Meteor UI 的 ironRouter 的鲨鱼分支,因为它使用{{>yield}}而不是{{yield}}.

你可以使用这个:

路由特定的数据上下文

Router.map(function() {
    this.route('template', data: function() { return Something.find() });
});

您基本上使用参数传递上下文data。这样做可能比使用更容易,{{#with context}}因为您可以使用更多动态数据,这些数据对于每条路线都不同。

您可能已经尝试过了,我有点不确定它是否会转到命名的产量模板。

为模板使用普通的模板助手

Template.templateInYieldName.helper = function() {
    return Something.find();
}

{{helper.name}}然后你可以在你的命名产量中使用类似的东西。

带有车把助手的全局数据上下文

如果您打算为所有路线使用数据,您可以使用 Handlebars 全局帮助器。IE

Handlebars.registerHelper('todaysDate', function() {
    return (new Date).toString();
});

然后只需{{todaysDate}}在您的任何模板中使用。您可以使用您的数据代替日期。

于 2013-12-27T07:29:50.313 回答