Friday, January 12, 2018

Layouts in express ejs

Greetings!

This is the 5th part of our Node EMS.

In my last post I used the same html layout for all the views. But it is still repeating. So one solution for it to use a layout engine. For express ejs we can use express-ejs-layouts.

$ git clone https://github.com/slmanju/node-ems.git
$ cd node-ems
$ npm install
$ npm run devstart

If you like to check the changes done in this tutorial
$ git checkout layout-with-ejs

Installation

$ npm install express-ejs-layouts --save

Usage

in app.js we add following,
var expressLayouts = require('express-ejs-layouts');
app.use(expressLayouts);
app.set('layout', 'layout/main');

Then we create main layout file and use <%-body%> to generate final html. All other html files will have only the body content.
out final main.ejs

No comments:

Post a Comment