Sunday, June 14, 2015

Using Layouts With EJS in NodeJS

Layouts are essential thing to arrange your views in a well defined way. For EJS template engine, we can use express-ejs-layouts package. First install express-ejs-layouts as a npm package.
Then configure express-ejs-layouts in your express application like below. In here, I have added default layout file which is _main.ejs, located at ~views/layouts folder. And also there is another layout file called _login.ejs. But according to our configurations, _main.ejs file will be the default one.
In here you can see I have added ejs tag "<%-body%>" in the _main.ejs template. In the left side you can see signin.ejs which will use default layout.
If you want to use another layout except default layout, only thing you have to do is add that in controller action like below.
Download Source Code from GitHub.

No comments:

Post a Comment