【问题标题】:Pass context to yield when using Meteor Iron Router使用 Meteor Iron Router 时将上下文传递给 yield
【发布时间】:2014-01-02 07:27:16
【问题描述】:

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

{{#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-router github 项目上以an issue 的身份发布了我的问题,但还没有得到任何解决方案。

不胜感激。

2014 年 1 月 1 日编辑: 所以我的代码如下所示:

// 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 个不同的部分模板,所以我必须将其放入所有模板中。

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

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

    但它可能适用于基于 Meteor UI 的 ironRouter 的鲨鱼分支,因为它使用 {{&gt;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 全局帮助器。即

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

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

    【讨论】:

    • 我绝对可以为模板定义数据上下文,但我这里要实现的是为命名的yield定义一个数据上下文,可以是几个不同的子模板。
    • 您可以将子模板放在一个更大的模板中,该模板有产量吗?这不一样吗?你能发布你正在使用的代码吗?
    • 感谢您的回复。抱歉更新晚了。我已将我正在使用的代码放在上面的问题中。
    猜你喜欢
    • 1970-01-01
    • 2014-05-30
    • 2015-08-30
    • 2016-10-31
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 2015-11-08
    • 2013-09-10
    相关资源
    最近更新 更多