ejs and express-ejs-layouts will be handling templating duties for us in this application. We'll need to make sure our Node app has some configuration setup:

// tell express where to look for static assets
app.use(express.static(__dirname + '/public'));

// set ejs as our templating engine
app.set('view engine', 'ejs');
app.use(expressLayouts);

After configuration, we can create a file called views/layout.ejs to use as the overall layout. It will know when to pull in page info when we use the <%- body %> tag.

Also, if you wanted to define another layout (let's say a views/sidebar-layout.ejs), you could pass that into the res.render() like so:

res.render('pages/contact', { layout: 'sidebar-layout' });

Chris Sevilleja

162 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.