Multiple View paths on Node.js + Express

前端 未结 5 1619
一向
一向 2020-12-02 15:25

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

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 16:05

    In addition to @user85461 answer, the require view part did not work for me. What i did: removed the path stuff and moved it all to a module i could require, patch.ViewEnableMultiFolders.js (Works with current express):

    function ViewEnableMultiFolders(app) {
        // Monkey-patch express to accept multiple paths for looking up views.
        // this path may change depending on your setup.
        var lookup_proxy = app.get('view').prototype.lookup;
    
        app.get('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);
        };
    }
    
    module.exports.ViewEnableMultiFolders = ViewEnableMultiFolders;
    

    and used:

    var Patch = require('patch.ViewEnableMultiFolders.js');
    Patch.ViewEnableMultiFolders(app);
    app.set('views', ['./htdocs/views', '/htdocs/tpls']);
    

提交回复
热议问题