Meteor - Setting the document title
Asked Answered
L

8

41

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

Lobation answered 26/12, 2012 at 5:15 Comment(0)
B
45

Pretty much anywhere in your client-side JavaScript code:

document.title = "My new title";
Baxy answered 26/12, 2012 at 9:46 Comment(0)
F
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");
Flightless answered 4/9, 2014 at 5:3 Comment(1)
Same as suggested here github.com/oortcloud/…Preferential
D
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
        }
      });
    }
  });
});
Defray answered 9/2, 2015 at 18:32 Comment(0)
E
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}}
Expeditious answered 25/9, 2013 at 16:52 Comment(0)
C
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";
    }
  });
});
Chasteen answered 7/10, 2014 at 16:21 Comment(0)
P
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>
Photometry answered 5/11, 2014 at 9:40 Comment(0)
P
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.

Putative answered 6/8, 2015 at 12:29 Comment(2)
May not matter to some, but Session variables will not survive a page refresh.Sulfur
@Durham, on a refresh the route will be run again.Ubiquitous
H
-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'
            }
          })
    }
})();
Hercule answered 19/12, 2015 at 16:1 Comment(1)
Because you posted an Angular.js example in a Meteor.js-related question thread.Dispute

© 2022 - 2024 — McMap. All rights reserved.