include.js
1.67 KB
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
/**
* guang
* @author: lcy<chuanyang.liu@yoho.cn>
* @date: 2016/09/01
*/
'use strict';
let fs = require('fs');
// exphbs = require('express-handlebars').create(),
// app = express(),
function wrap(app, exphbs) {
let vPath = app.get('views') || 'views/',
extName = app.get('view engine') || '.hbs',
Handlebars = exphbs.handlebars;
Handlebars.registerHelper('include', function(partial, options) {
console.log('-------');
// console.log(exphbs);
// console.log(exphbs);
// console.log('==================');
// console.log(Handlebars.partials);
let tmpl = Handlebars.partials[partial] || exphbs.partials[partial];
// let encoding = options.hash['encoding'] || 'utf8';
exphbs.getPartials().then(function(partials) {
console.log(partials.length);
}).catch(function() {
});
if (tmpl === null) {
let source = fs.readFileSync(`${vPath}/${partial}${extName}`, 'utf8');
if (source !== null) {
Handlebars.registerPartial(partial, source);
tmpl = Handlebars.partials[partial];
} else {
new Error('partial is not found : ' + partial);
}
}
// options.fn(this);
let ctx = Object.create(this);
for (let i in options.hash) {
if (options.hash.hasOwnProperty(options.hash[i])) {
ctx[i] = options.hash[i];
}
}
if (typeof tmpl !== 'function') {
tmpl = Handlebars.compile(tmpl);
}
return new Handlebars.SafeString(tmpl(ctx, options));
});
}
module.exports = wrap;