How to use multiple layouts in a SailsJS application?

Sails.js has a separate frontend and admin layout. My view engine is ejs .

How to use separate layouts for the admin interface and site?

Can I use a specific layout for each action?

+7
layout ejs
source share
5 answers

From the Sails.js Documentation :

At least in EJS, instead of specifying your custom layout with a local location, you should use _layoutFile:

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

The path to the layout you want to use should be relative to your rendering. Therefore, if you work with the UserController action by creating 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:

The documentation seems to be a bit off the code, so for the current version (v0.9.8), the path to the following is:

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

In Sails 0.10, you can set a different layout in your controller:

 // override the layout to use another res.locals.layout = 'layouts/layout2'; return res.view('test'); 

Detailed explanation here .

+3
source share

In Sails v0.12, if you need to install a layout in your controller, you can create a new folder (for example, a β€œlayout”) and click the layout files.

 res.view('auth/login', {layout: layout/my_file_layout} 

You can also do:

 res.locals.layout = "layout/my_file_layout"; res.view('auth/login'); 
+2
source share

Sorry for posting my question, but I need a similar solution for my application. It would be great to define layouts in routes.js so that, for example, / admin / * uses one layout and, for example. / app / * will use another, etc. Because it’s lavash, plunging into controllers and overloading them with static layouts. I tried this concept, but it seems to work only if I also define a controller in the routes.js configuration file , for example:

 module.exports.routes = { '/admin/*' : { controller: 'AdminController', action: 'index', locals: { layout: 'admin/layout' } } }; 

This works, but directs all actions for the administrator to the same controller, which, of course, is wrong. If I omit part of the controller, this concept always uses the default view /layout.ejs, and the local one is not set:

 module.exports.routes = { '/admin/*' : { locals: { layout: 'admin/layout' } } }; 

This does not work, but it would be ideal to get it that way.

0
source share

If you need to set the view and location in the controller (Sails v0.11):

 res.view('auth/login', { layout: null } ); 

Markup:

null = None

'folder / layout' = Your layout

0
source share

All Articles