Created
May 13, 2013 14:50
-
-
Save poying/5568859 to your computer and use it in GitHub Desktop.
express 多個 view folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var path = require('path'); | |
var express = require('express'); | |
function _merge(a, b) { | |
if (a && b) { | |
var i; | |
for (i in b) { | |
if (b.hasOwnProperty(i)) { | |
a[i] = b[i]; | |
} | |
} | |
} | |
return a; | |
} | |
express.application.render = function (name, options, fn) { | |
var opts = {}; | |
var cache = this.cache; | |
var engines = this.engines; | |
var view, key; | |
// support callback function as second arg | |
if ('function' === typeof options) { | |
fn = options; | |
options = {}; | |
} | |
// merge app.locals | |
_merge(opts, this.locals); | |
// merge options._locals | |
if (options._locals) { | |
utils.merge(opts, options._locals); | |
} | |
// merge options | |
_merge(opts, options); | |
// set .cache unless explicitly provided | |
opts.cache = null === opts.cache | |
? this.enabled('view cache') | |
: opts.cache; | |
// primed cache | |
if (opts.cache) { | |
view = cache[name]; | |
} | |
// view | |
if (!view) { | |
var views = this.get('views'); | |
var tmp = name.split('<-'); | |
var root = views.hasOwnProperty(tmp[0]) ? views[tmp[0]] : views.default; | |
view = new (this.get('view'))(name, { | |
defaultEngine: this.get('view engine'), | |
root: root, | |
engines: engines | |
}); | |
if (!view.path) { | |
var err = new Error('Failed to lookup view "' + name + '"'); | |
err.view = view; | |
return fn(err); | |
} | |
// prime the cache | |
if (opts.cache) { | |
cache[name] = view; | |
} | |
} | |
// render | |
try { | |
view.render(opts, fn); | |
} catch (err2) { | |
fn(err2); | |
} | |
}; | |
module.exports = express; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.render('admin<-index') // 使用 key 是 admin 的位置
.render('index') // 使用 key 是 default 的位置
要改有點麻煩,所以直接把 express 程式碼複製出來在修改,這樣比較快...