meteor

Routing

Routing with Iron Router

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: https://iron-meteor.github.io/iron-router/

With FlowRouter

FlowRouter is more modular compared to Iron Router.

Install FlowRouter

meteor add kadira:flow-router

Rendering a template

In particular, you must manually add a layout rendering package to link with your rendering engine:

Then you can render through dynamic templating (in the case of Blaze):

<template name="mainLayout">
  {{> Template.dynamic template=area}}
</template>
FlowRouter.route('/blog/:postId', {
  action: function (params) {
    BlazeLayout.render("mainLayout", {
      area: "blog"
    });
  }
});

Rendering a template with parameters and/or query

The parameters are specified on the route, like with Iron Router:

FlowRouter.route("/blog/:catId/:postId", {
  name: "blogPostRoute",
  action: function (params) {
    //...
  }
})

But the parameters are not passed as data context to the child template. Instead, the child template must read them:

// url: /blog/travel/france?showcomments=yes
var catId = FlowRouter.getParam("catId"); // returns "travel"
var postId = FlowRouter.getParam("postId"); // returns "france"

var color = FlowRouter.getQueryParam("showcomments"); // returns "yes"

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow