|
|
@ -11,27 +11,6 @@ function viewsMiddleware(path, { engineSource = consolidate, extension = "html", |
|
|
|
return function views(ctx, next) { |
|
|
|
if (ctx.render) return next() |
|
|
|
|
|
|
|
ctx.getRender = function (relPath, locals = {}) { |
|
|
|
return getPaths(path, relPath, extension).then(paths => { |
|
|
|
const suffix = paths.ext |
|
|
|
const state = Object.assign(locals, options, ctx.state || {}) |
|
|
|
state.partials = Object.assign({}, options.partials || {}) |
|
|
|
|
|
|
|
if (isHtml(suffix) && !map) { |
|
|
|
return send.getBody(ctx, paths.rel, { root: path }) |
|
|
|
} |
|
|
|
|
|
|
|
const engineName = map && map[suffix] ? map[suffix] : suffix |
|
|
|
const render = engineSource[engineName] |
|
|
|
|
|
|
|
if (!engineName || !render) { |
|
|
|
return Promise.reject(new Error(`Engine not found for the ".${suffix}" file extension`)) |
|
|
|
} |
|
|
|
|
|
|
|
return render(resolve(path, paths.rel), state) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// 将 render 注入到 context 和 response 对象中
|
|
|
|
ctx.response.render = ctx.render = function (relPath, locals = {}) { |
|
|
|
return getPaths(path, relPath, extension).then(paths => { |
|
|
|