How to use multiple layout within a SailsJS app?

anhnt picture anhnt · Jan 7, 2014 · Viewed 7.3k times · Source

My Sails.js application have separate frontend and admin layout. My view engine is ejs.

How do I use separate layouts for frontend and admin site?

Can I use specific layout for each action?

Answer

bredikhin picture bredikhin · Jan 7, 2014

From Sails.js Documentation:

At least in EJS, instead of indicating your custom layout with the layout local, you must use _layoutFile:

res.view({
  _layoutFile: 'relativePathToYourCustomLayoutFromTheTargetView.ejs'
});

The path to the layout you're wanting to use should be specified relative to the view you're rendering. So if you're in the create action of the UserController, rendering a view (views/user/create.ejs), the relative path to your custom layout might be: ../staticSiteLayout.ejs

PROJECT FOLDER
└── views
    ├── staticSiteLayout.ejs
    ├── layout.ejs
    └── user
        └── create.ejs

UPDATE:

Seems like the documentation is a bit off from the code, so for the current (v0.9.8) version the way to go is the following:

module.exports = { 
  index: function(req, res){
    res.view({ layout: 'layoutadmin' });
  }
}