Node.js + Express上的多个查看路径 [英] Multiple View paths on Node.js + Express

查看:106
本文介绍了Node.js + Express上的多个查看路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Express Framework 在 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.

我希望每个模块都将其文件放在单独的文件夹 strong>包括查看文件
任何人都知道如何实现?

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)
    };
}

您将通过调用上述功能启用新的逻辑,并通过作为参数,然后您可以指定一组视图的配置:

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 将是未定义

Unluckily the above solution won't work in Express 3.x since express.view would be undefined

另一个可能的解决方案是代理 response.render 函数,并设置views文件夹配置,直到得到匹配:

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);
};

我没有测试过,对我来说感觉很骇人听闻,不幸的是这个功能被推再次返回:
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上的多个查看路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆