How do I set the title of a page/route with express and jade?
node.js express set title
simple.jade:
!!! 5
title= title
express application:
app.get('/simple',function(req,res) {
res.render('simple',{title='mytitle'});
}
I think the jade should be {title}. What you did doesn't work for me –
Borate
I think I put an extra space in, it should be
title= title
–
Debonair 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 local –
Debonair
Detailed info about locals in views is in the Express guide: expressjs.com/guide.html#view-rendering –
Anibalanica
You probably need to restart your server as well. –
Laroche
in 2020, its res.render('index', { title: 'Page Title' }); –
Diagonal
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' });
});
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'
});
});
In your server (app.js):
app.set('title', 'My Site');
How to render this in ejs template? title is not defined –
Inhospitable
© 2022 - 2024 — McMap. All rights reserved.