简体   繁体   English

流星:带有参数的渲染模板

[英]meteor: render template with arguments

I'm using iron routes and in my router I have the following to render a specific template 我正在使用铁路线,并且在路由器中有以下内容可以渲染特定模板

var Home = RouteController.extend({
    ....
    action: function () {
        if (this.ready()) {
          this.render('main', {state: 'normal'});
        }
        else {
          ;//this.render('loading');
        }
    }
});

As you can see I want to pass a state variable to the template which is used in the class attribute as follows 如您所见,我想将state变量传递给在class属性中使用的模板,如下所示

<template name="main">
    <section class="{{state}}">
        ....
    </section>
</template>

However, this state variable is undefined , meaning that what I'm trying here doesn't work. 但是,这个状态变量是undefined ,这意味着我在这里尝试的不起作用。 Any suggestions how I can pass data to the template ? 有什么建议可以将数据传递到模板吗?

I think using the data option would be your best bet. 我认为使用数据选项将是您最好的选择。

var Home = RouteController.extend({
    data:{state:'normal'},
    action: function () {
        if (this.ready()) {
          this.render('main');
        }
        else {
          ;//this.render('loading');
        }
    }
});

data can also be a function that if it contains a reactive data source will rerun each time the data changes. 数据还可以是一个功能,如果它包含反应性数据源,则每次数据更改时都会重新运行。

A couple notes though. 不过有几个注意事项。 if you have a loadingTemplate defined for the route and are returning your subscriptions from wait on.. iron-router will handle rendering the loading template for you. 如果您为该路由定义了loadingTemplate ,并且正在从等待中返回您的订阅,则iron-router会为您处理渲染加载模板。

Also the data option is designed to return a single document that when does not exist iron-router will render the notFound template for that route. 同样,data选项还设计为返回单个文档,该文档在不存在时Iron-Router将呈现该路由的notFound模板。 Template state should really handled by template helpers. 模板状态应真正由模板助手处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM