簡體   English   中英

為EXPRESS中的Jade文件添加更多視圖文件夾?

[英]Add more view folder for Jade files in EXPRESS?

我知道這行app.set('views', path.join(__dirname, 'views')); 在Express中設置視圖變量,這樣我就可以在./views文件夾中呈現我的所有.jade文件。

有誰知道如何為views變量增加更多的值,以便我可以在./views文件夾之外以及..views文件夾內部渲染.jade文件?

(例如../includes/form/form.jade和./views/index.jade)

我有Express 4.2.0,其文件結構如下:

+ node_modules
+ public
    - includes
        - form
            - index.jade
            - style.less
            - script-form.js
+ views
    - index.jade

這對我有用,因為我已經表達了3.x或更高版本。

function enable_multiple_view_folders() {
// Monkey-patch express to accept multiple paths for looking up views.
// this path may change depending on your setup.
var View = require("./node_modules/express/lib/view"),
    lookup_proxy = View.prototype.lookup;

View.prototype.lookup = function(viewName) {
    var context, match;
    if (this.root instanceof Array) {
        for (var i = 0; i < this.root.length; i++) {
            context = {root: this.root[i]};
            match = lookup_proxy.call(context, viewName);
            if (match) {
                return match;
            }
        }
        return null;
    }
    return lookup_proxy.call(this, viewName);
};

}

並像這樣使用它:

enable_multiple_view_folders();
app.set('views', [__dirname + '/views', __dirname + '/form']);
app.set('view engine', 'jade');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM