问题描述
我正在使用 Express 框架的 Node.js 编写 CMS.在我的 CMS 上,我有几个用于用户、页面等的模块.
I'm writing a CMS on Node.js with Express Framework. On my CMS I have several modules for users, pages, etc.
我希望每个模块在单独的文件夹中都有他的文件,包括视图文件.有谁知道我怎样才能做到这一点?
I want that each module will have his files on separate folder, including the view files.Anyone know how can I achieve that?
我使用 swig 作为我的模板引擎,但如果有帮助,我可以将其替换为其他内容.
I'm using swig as my template engine but I can replace it to something else if it will helps.
推荐答案
最后更新
框架从 Express 4.10 开始支持多视图文件夹功能
只需将位置数组传递给 views
属性,就像这样.
Just pass an array of locations to the views
property, like so.
app.set('views', [__dirname + '/viewsFolder1', __dirname + '/viewsFolder2']);
Express 2.0
据我所知,express 目前不支持多个视图路径或命名空间(就像静态中间件那样)
As far as I know express doesn't support multiple view paths or namespaces at the moment (like the static middleware do)
但是您可以自己修改查找逻辑,使其按您想要的方式工作,例如:
But you can modify the lookup logic yourself so that it works the way you want, for example:
function enableMultipleViewFolders(express) {
// proxy function to the default view lookup
var lookupProxy = express.view.lookup;
express.view.lookup = function (view, options) {
if (options.root instanceof Array) {
// clones the options object
var opts = {};
for (var key in options) opts[key] = options[key];
// loops through the paths and tries to match the view
var matchedView = null,
roots = opts.root;
for (var i=0; i<roots.length; i++) {
opts.root = roots[i];
matchedView = lookupProxy.call(this, view, opts);
if (matchedView.exists) break;
}
return matchedView;
}
return lookupProxy.call(express.view, view, options)
};
}
您将通过调用上面的函数并将express 作为参数来启用新逻辑,然后您将能够为配置指定一组视图:
You will enable the new logic by calling the function above and passing express as a parameter, and then you will be able to specify an array of views to the configuration:
var express = require('express');
enableMultipleViewFolders(express);
app.set('views', [__dirname + '/viewsFolder1', __dirname + '/viewsFolder2']);
或者,如果您愿意,可以直接修补框架(更新其中的 view.js 文件)
Or, if you prefer, you can patch the framework directly (updating the view.js file inside it)
这应该适用于 Express 2.x,不确定它是否适用于新版本 (3.x)
This should work in Express 2.x, not sure if it will with the new version (3.x)
更新
不幸的是,上述解决方案在 Express 3.x 中不起作用,因为 express.view 将是 undefined
Unluckily the above solution won't work in Express 3.x since express.view would be undefined
另一种可能的解决方案是代理 response.render 函数并设置视图文件夹配置,直到匹配为止:
Another possible solution will be to proxy the response.render function and set the views folder config until it gets a match:
var renderProxy = express.response.render;
express.render = function(){
app.set('views', 'path/to/custom/views');
try {
return renderProxy.apply(this, arguments);
}
catch (e) {}
app.set('views', 'path/to/default/views');
return renderProxy.apply(this, arguments);
};
我没有测试过,反正我觉得很hacky,不幸的是这个功能又被推迟了:https://github.com/visionmedia/express/pull/1186
I've not tested it, it feels very hacky to me anyway, unluckily this feature has been pushed back again:https://github.com/visionmedia/express/pull/1186
更新 2
Express 4.10 中添加了此功能,因为以下拉取请求已合并:https://github.com/strongloop/express/pull/2320
This feature has been added in Express 4.10, since the following pull request has been merged:https://github.com/strongloop/express/pull/2320
这篇关于Node.js + Express 上的多个视图路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!