Meteor – multi-pages applications

Posted by on Sep 10, 2013 in Javascript, Meteor | 2 Comments

Meteor allows you to create multi-pages applications like mine (ReadingList), to do so I advise you to install Meteorite which is a package manager for Meteor that allows you to use non core packages. After installing it you will need to two very usefull package : router and mini-pages.

Router and mini-page gives you the tools you need to use routing system for your pages in your application.  Lets see how it works :

First of all you need to define a layout template :

This layout template is called on all the pages and I use the keyword {{{yield}}} to specify where the herited template have to display their content. After doing this, I can go to my .js file and define all my routes :

This snippet is from my Meteor application Reading list, and it manage all I need to for my pages routing. The Meteor.pages function is the routing function, to use it I specify the path of my page and the template I want to use. Here, I have defined the landing page to index and the /users to my user_list template.

The routing function allows you to define a lots of usefull options about the route, for exemple I can specify a function to call before routing the user. It is what I do in the last route, I call the setUser function before routing the user to the page. The setUser function just get the ‘safename’ parameter in the URL (via context.params.safename) find the user with this safename and set this user in the session before calling the ‘user_show_reading_list’ template.

What I want in my ‘user_show_reading_list’ template is to get the user in the session to display the right information about him, to do that I use a template helper :

With this, when a user use a link like : users/myUserName the application automaticaly get the myUserName informations in the session and I can use them in my template :

In this snippet, I use the keyword with that calls the helper to get  the user and I display his username.

This is a quick exemple of what you can do with the router + mini-page package, and how you can use them to create a multi-pages application. I advise you to check the packages documentation to learn more about routing in Meteor.