简体   繁体   中英

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

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

However, this state variable is undefined , meaning that what I'm trying here doesn't work. 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.

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. Template state should really handled by template helpers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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