-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
layoutshim.js
79 lines (66 loc) · 2.71 KB
/
layoutshim.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
79
/**
* Module dependencies
*/
var path = require('path');
var semver = require('semver');
/**
* Implement EJS layouts (a la Express 2)
*
* TODO:
* Extrapolate this functionality to a separate hook
* to make it easier for folks to extend it with support
* for other view engines (e.g. hbs)
*
* @param {Sails} sails
* @param {Function} cb
*/
module.exports = function layoutshim (sails, cb) {
// If layout config is set, attempt to use view partials/layout
if (sails.config.views.layout) {
// If `http` hook is not enabled, we can't use partials
// (depends on express atm)
if (sails.config.hooks.http) {
// Get the view engine name
var engineName = sails.config.views.engine.name || sails.config.views.engine.ext;
// Use ejs-locals for all ejs templates
if (engineName === 'ejs') {
var ejsLayoutEngine = require('ejs-locals');
// Wait until express is ready, then configure the view engine
return sails.after('hook:http:loaded', function () {
sails.log.verbose('Overriding ejs engine config with ejslocals to implement layout support...');
sails.config.views.engine.fn = ejsLayoutEngine;
cb();
});
}
else if (engineName === 'handlebars') {
var exphbs = require('express-handlebars');
var handlebarsMajorVersion;
try {
handlebarsMajorVersion = semver.major(require('express-handlebars/node_modules/handlebars').VERSION);
} catch (e) {
try {
handlebarsMajorVersion = semver.major(require('handlebars').VERSION);
}
catch (e2) {
handlebarsVersion = null;
}
}
if (!handlebarsMajorVersion) {sails.log.warn('Could not determine Handlebars version; versions < 4.0.0 may introduce security risks (see https://snyk.io/test/npm/handlebars/3)');}
else if (handlebarsMajorVersion < 4) {
sails.log.warn('Using Handlebars v' + handlebarsMajorVersion + '.x; Versions < 4.0.0 may introduce security risks (see https://snyk.io/test/npm/handlebars/3).');
sails.log.warn('When an updated version becomes available, you should be able to install it by updating your copy of Sails with `npm update`.');}
return sails.after('hook:http:loaded', function() {
sails.log.verbose('Overriding handlebars engine with express-handlebars to implement layout support...');
var hbs = exphbs.create({
defaultLayout: path.join('..', (sails.config.views.layout + '.' + (sails.config.views.extension || 'handlebars')) || ''),
helpers: sails.config.views.helpers || {},
partialsDir: path.join('views', sails.config.views.partials || '')
});
sails.config.views.engine.fn = hbs.engine;
cb();
});
}
}
}
return cb();
};