meteor Routing Routing with Iron Router


Example

Install Iron Router

From the terminal:

meteor add iron:router

Basic configuration

Router.configure({
    //Any template in your routes will render to the {{> yield}} you put inside your layout template 
    layoutTemplate: 'layout',
    loadingTemplate: 'loading'
});

Render without data

//this is equal to home page
Router.route('/', function (){
    this.render('home')
});

Router.route('/some-route', function () {
    this.render('template-name');
});

Render with data and parameters

Router.route('/items/:_id', function () {
    this.render('itemPage', {
        data: function() {
            return Items.findOne({_id: this.params._id})
        }
    });
});

Render to a secondary yield

Router.route('/one-route/route', function() {
    //template 'oneTemplate' has {{> yield 'secondary'}} in HTML
    this.render('oneTemplate');
    
    //this yields to the secondary place
    this.render('anotherTemplate', {
        to: 'secondary'
    });

    //note that you can write a route  for '/one-route' 
    //then another for '/one-route/route' which will function exactly like above.
});

Subscribe and wait for data before rendering template

Router.route('/waiting-first', {
    waitOn: function() {
        //subscribes to a publication 
        //shows loading template until subscription is ready
        return Meteor.subscribe('somePublication')
    },

    action: function() {
        //render like above examples
    }
});

Subscribe to multiple publications and wait for data before rendering template

Router.route('/waiting-first', {
    waitOn: function() {
        //subscribes to a publication 
        //shows loading template until subscription is ready
        return [Meteor.subscribe('somePublication1'),Meteor.subscribe('somePublication2')];
    },

    action: function() {
        //render like above examples
    }
});

Guide for Iron Router: http://iron-meteor.github.io/iron-router/