How to use multiple layout within a SailsJS app?

前端 未结 5 728
盖世英雄少女心
盖世英雄少女心 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: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' });
      }
    }
    

提交回复
热议问题