41

Is there a way to change the <title> element in a Meteor app? Seems templates are only processed in the <body>.

Dean Brundage
  • 2,038
  • 18
  • 31

8 Answers8

45

Pretty much anywhere in your client-side JavaScript code:

document.title = "My new title";
David Wihl
  • 1,491
  • 13
  • 14
38

You can extend David Wihl's solution:

Deps.autorun(function(){
  document.title = Session.get("DocumentTitle");
});

Then You can in any time call:

Session.set("DocumentTitle","New Document Title");
KNOFF
  • 441
  • 4
  • 4
  • Same as suggested here https://github.com/oortcloud/unofficial-meteor-faq#how-do-i-get-reactive-html-in-the-head-tag – jefeman Dec 04 '14 at 02:49
12

If you use iron:router you can add the package manuelschoebel:ms-seo to handle the title along with meta tags. This is useful for static and dynamic SEO data:

Router.map(function() {
  return this.route('blogPost', {
    path: '/blog/:slug',

    onAfterAction: function() {
      var post = this.data().post;
      SEO.set({
        title: post.title,
        meta: {
          'description': post.description
        },
        og: {
          'title': post.title,
          'description': post.description
        }
      });
    }
  });
});
jrbedard
  • 3,662
  • 5
  • 30
  • 34
10

You can create a helper for setting the title (CoffeeScript):

UI.registerHelper "setTitle", ->
  title = ""
  for i in [0..arguments.length-2]
    title += arguments[i]
  document.title = title
  undefined

or the same in Js:

UI.registerHelper("setTitle", function() {
  var title = "";
  for (var i = 0; i < arguments.length - 1; ++i) {
    title += arguments[i];
  }
  document.title = title;
});

Then you can use it in complex ways, and it will be reactive:

{{#if book}}
  {{setTitle book.title}}
{{else}}
  {{setTitle "My books"}}
{{/if}}
ZucchiniZe
  • 301
  • 2
  • 14
Bernát
  • 1,362
  • 14
  • 19
8

I find it more convenient to handle that kind of thing directly in the router with a onBeforeAction:

Router.map(function() {
  return this.route('StudioRoot', {
    path: '/',
    onBeforeAction: function() {
      return document.title = "My Awesome Meteor Application";
    }
  });
});
Julien Le Coupanec
  • 7,742
  • 9
  • 53
  • 67
3

you can also include in <head> </head> tags which does not reside in a template. try this:

contents of sample.html:

<head>
    <title>my title</title>
</head>

<body>
    ...
</body>

<template name="mytemplate">
    ...
</template>
gok
  • 39
  • 1
1

What I ended up doing:

in the Meteor.isClient:

Meteor.startup(function() {
    Deps.autorun(function() {
        document.title = Session.get('documentTitle');
    });
});

now that the var is set reactively, go in the router file (if not already done: meteor add iron:router. My router file is both client and server)

Router.route('/', {
    name: 'nameOfYourTemplate',
    onBeforeAction: function () {
        Session.set('documentTitle', 'whateverTitleIWant');
        this.next();    //Otherwise I would get no template displayed
    }
});

It doesn't matter if you already set a title in the head tag. It will be replaced by this one according to your route.

Leths
  • 111
  • 3
-1

I had to look for an answer that would work for ui-router. I know that this might not be the answer you were looking for. Since this question was posted about 2 years ago, I figured if someone else was to come here looking for a solution with ui-router, this answer could help them:

myApp.run.js

(function() {
  'use strict';

  angular
    .module('myApp')
    .run(run);

  run.$inject = ['$rootScope', '$state'];

  function run($rootScope, $state) {
    $rootScope.$on("$stateChangeSuccess", function(previousRoute, currentRoute){
      document.title = 'myApp - ' + currentRoute.data.pageTitle;
    });
  };

})();

routes.js

(function() {
    'use strict';

    angular
      .module('myApp')
      .config(config);

    config.$inject = 
      ['$urlRouterProvider', '$stateProvider', '$locationProvider'];

    function config($urlRouterProvider, $stateProvider) {

        // ...
        $stateProvider
          .state('home', {
            url: '/',
            templateUrl: 'client/home/views/home.ng.html',
            controller: 'HomeController',
            data: {
              pageTitle: 'My Dynamic title'
            }
          })
    }
})();
Austin Perez
  • 587
  • 7
  • 27