How to use multiple layout within a SailsJS app?

前端 未结 5 729
盖世英雄少女心
盖世英雄少女心 2021-02-15 13:16

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

How do I use separate layouts for frontend

相关标签:
5条回答
  • 2021-02-15 13:40

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

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

    Detailed explanation here.

    0 讨论(0)
  • 2021-02-15 13:41

    In Sails v0.12, if you need to set layout in your controller, you might create a new folder (e.g. "layout") and push 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');
    
    0 讨论(0)
  • 2021-02-15 13:44

    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' });
      }
    }
    
    0 讨论(0)
  • 2021-02-15 14:00

    Sorry for spamming your question but i need a similar solution for my app. It would be wonderful to define layouts in routes.js so for example /admin/* would use one layout, and eg. /app/* would use the other, etc. Because it's a pita do dive into controllers and overspam them with static layout paths. I tried this concept but it seems to be working only if I also define a controller in the routes.js config file eg:

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

    This does work, but is routing all actions for admin into the same controller, which is of course wrong. If I omit the controller part, that concept is always using the default view/layout.ejs and the local is not set:

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

    This doesn't work but it would be ideal to get it that way.

    0 讨论(0)
  • 2021-02-15 14:04

    If you need to set view AND layout in your controller (Sails v0.11):

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

    Layout:

    null = None

    'folder/layout' = Your Layout

    0 讨论(0)
提交回复
热议问题