Jeanluca Scaljeri
Jeanluca Scaljeri

Reputation: 29097

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 ?

Upvotes: 0

Views: 390

Answers (1)

Kelly Copley
Kelly Copley

Reputation: 3158

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.

Upvotes: 1

Related Questions