Locals : informations que l'on veut retrouver dans toutes les pages.
var express = require('express'); var app = express(); app.set('view engine', 'ejs'); app.locals.pagetitle = "Awesome Website"; app.get('/', function(req, res) { console.log('/ requested'); res.render('default', { title: 'Hello', content: 'Heyyy salut-donc !', users: ['Mireille', 'José', 'Marcel', 'Odette', 'Lucien', 'Félicienne'] }); }) // si on a autre chose app.get('*', function(req, res) { console.log('Page inconnue demandée'); res.render('default', { title: '😭', content: '😭😭😭'}); }) var server = app.listen(3000, function() { console.log('Listening on port 3000.'); })
<!DOCTYPE html> <html> <head><% include partials/head.ejs %></head> <body> <h1><%= pagetitle %> - <%= title %></h1> <p><%= content %></p> <% if (typeof(users) !== 'undefined') { %> <ul> <% for (var i=0; i <users.length; i++) {%> <li><%= users[i] %></li> <% } %> </ul> <% } %> </body> </html>