This repository has been archived by the owner on Jan 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexpressView.js
81 lines (65 loc) · 3.71 KB
/
expressView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*───────────────────────────────────────────────────────────────────────────*\
│ Copyright (C) 2014 eBay Software Foundation │
│ │
│hh ,'""`. │
│ / _ _ \ Licensed under the Apache License, Version 2.0 (the "License"); │
│ |(@)(@)| you may not use this file except in compliance with the License. │
│ ) __ ( You may obtain a copy of the License at │
│ /,'))((`.\ │
│(( (( )) )) http://www.apache.org/licenses/LICENSE-2.0 │
│ `\ `)(' /' │
│ │
│ Unless required by applicable law or agreed to in writing, software │
│ distributed under the License is distributed on an "AS IS" BASIS, │
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
│ See the License for the specific language governing permissions and │
│ limitations under the License. │
\*───────────────────────────────────────────────────────────────────────────*/
"use strict";
var resolver = require('file-resolver');
var util = require('./util');
var proto = {
get path() {
// Unfortunately, since we don't know the actual file to resolve until
// we get request context (in `render`), we can't say whether it exists or not.
return true;
},
render: function (options, callback) {
var locals, view, engine;
locals = options && options.context;
view = this.resolver.resolve(this.name, util.localityFromLocals(locals));
// This is a bit of a hack to ensure we override `views` for the duration
// of the rendering lifecycle. Unfortunately, `adaro` and `consolidate`
// (https://github.com/visionmedia/consolidate.js/blob/407266806f3a713240db2285527de934be7a8019/lib/consolidate.js#L214)
// check `options.views` but override with `options.settings.views` if available.
// So, for this rendering task we need to override with the more specific root directory.
options.settings = Object.create(options.settings);
options.views = options.settings.views = view.root;
engine = this.engines['.' + this.defaultEngine];
engine(view.file, options, callback);
}
};
function buildCtor(fallback) {
function View(name, options) {
this.name = name;
this.root = options.root;
this.defaultEngine = options.defaultEngine;
this.engines = options.engines;
this.resolver = resolver.create({ root: options.root, ext: this.defaultEngine, fallback: fallback });
}
View.prototype = proto;
View.prototype.constructor = View;
return View;
}
module.exports = function () {
var view;
return function (req, res, next) {
var config = req.app.kraken;
//if the view engine is 'js and if it has not been overridden already
if (config.get('express:view engine') === 'js' && !view) {
view = buildCtor(config.get('i18n:fallback'));
req.app.set('view', view);
}
next();
};
};