node.js express set title
Asked Answered
B

4

14

How do I set the title of a page/route with express and jade?

Borate answered 18/3, 2011 at 1:26 Comment(0)
D
12

simple.jade:

!!! 5
 title= title

express application:

app.get('/simple',function(req,res) {
    res.render('simple',{title='mytitle'});
}
Debonair answered 18/3, 2011 at 2:14 Comment(7)
I think the jade should be {title}. What you did doesn't work for meBorate
I think I put an extra space in, it should be title= titleDebonair
should it not be {locals: {title='title'}} ?Pena
{locals: {title='title'}} works, but newer versions interpret any key that isn't an express command as a localDebonair
Detailed info about locals in views is in the Express guide: expressjs.com/guide.html#view-renderingAnibalanica
You probably need to restart your server as well.Laroche
in 2020, its res.render('index', { title: 'Page Title' });Diagonal
A
5

Specifying the page title in the route is easiest method.

This example shows the index.js file in my routes folder.. which is the default set by Express.

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Page Title' });
});
Aigrette answered 20/1, 2015 at 23:45 Comment(0)
A
3

This is what I did and it worked for me. The example uses a hypothetical "videos" view that needs a title to be "video gallery", adjust accordingly.

layout.jade //This is added by default in express apps

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    block content

videos.jade //You can create a view such as this

extends layout

block content
  h1= title

app.js //The file is default but you must add a route like this. And set the title

app.get('/videos/', function(req, res){
  res.render('videos', {
    title: 'Video Gallery'
  });
});
Anyway answered 24/12, 2013 at 0:17 Comment(0)
F
1

In your server (app.js):

app.set('title', 'My Site');
Fascine answered 24/1, 2019 at 22:25 Comment(1)
How to render this in ejs template? title is not definedInhospitable

© 2022 - 2024 — McMap. All rights reserved.