Now that we have the routes of our posts working, we are only missing the right titles being displayed for each page.
Sadly, <head></head>
is not a reactive template in Meteor, where we could let Meteor do the work of changing titles and meta tags.
To change the document title, we need to come up with a different way of changing it, based on the current route.
Luckily, iron:router
has the onAfterAction()
function, which can also be used in the Router.configure()
function to run before every route. In this function, we have access to the data context of the current route, so we can simply set the title using native JavaScript:
Router.configure({ layoutTemplate: 'layout', notFoundTemplate: 'notFound', onAfterAction: function() { var data = Posts.findOne({slug: this.params.slug}); if(_.isObject(data) && !_.isArray(data)) ...