-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
55 lines (46 loc) · 1.42 KB
/
app.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
'use strict';
const path = require('path');
const fs = require('mz/fs');
const handlebars = require('handlebars');
const COMPILE = Symbol('compile');
module.exports = app => {
const partials = loadPartial(app);
if (partials) {
for (const key of Object.keys(partials)) {
handlebars.registerPartial(key, partials[key]);
}
}
class HandlebarsView {
constructor(ctx) {
this.app = ctx.app;
}
async render(name, context, options) {
const content = await fs.readFile(name, 'utf8');
return this[COMPILE](content, context, options);
}
async renderString(tpl, context, options) {
return this[COMPILE](tpl, context, options);
}
[COMPILE](tpl, context, options) {
return handlebars.compile(tpl, Object.assign({}, this.app.config.handlebars, options))(context);
}
}
app.view.use('handlebars', HandlebarsView);
};
function loadPartial(app) {
const partialsPath = app.config.handlebars.partialsPath;
// istanbul ignore next
if (!fs.existsSync(partialsPath)) return;
const partials = {};
const files = fs.readdirSync(partialsPath);
for (let name of files) {
const file = path.join(partialsPath, name);
const stat = fs.statSync(file);
if (!stat.isFile()) continue;
name = name
.replace(/\.\w+$/, '')
.replace(/[_-][a-z]/ig, s => s.substring(1).toUpperCase());
partials[name] = fs.readFileSync(file).toString();
}
return partials;
}