如何在SailsJS应用程序中使用多个布局? [英] How to use multiple layout within a SailsJS app?
问题描述
我的Sails.js应用程序具有单独的frontend
和admin
布局.
我的视图引擎是ejs
.
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?
推荐答案
来自 Sails.js文档:
至少在EJS中,必须使用_layoutFile来代替使用本地布局指示自定义布局:
At least in EJS, instead of indicating your custom layout with the layout local, you must use _layoutFile:
res.view({
_layoutFile: 'relativePathToYourCustomLayoutFromTheTargetView.ejs'
});
您要使用的布局路径应相对于要渲染的视图指定.因此,如果您在UserController的create动作中,呈现一个视图(views/user/create.ejs),则自定义布局的相对路径可能是:../staticSiteLayout.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
更新:
似乎文档与代码有些偏离,因此对于当前(v0.9.8)版本,解决方法如下:
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' });
}
}
这篇关于如何在SailsJS应用程序中使用多个布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!