-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (44 loc) · 1.06 KB
/
index.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
/**
* Pug driver for Sapling
*/
/* Dependencies */
import path from 'node:path';
import pug from 'pug';
import Interface from '@sapling/sapling/drivers/render/Interface.js';
import _ from 'underscore';
import SaplingError from '@sapling/sapling/lib/SaplingError.js';
export default class Pug extends Interface {
/**
* Initialise Pug
*/
constructor(App, viewsPath) {
super();
this.app = App;
this.viewsPath = viewsPath;
this.tags = {};
this.engine = pug;
}
/**
* Render a template file
*
* @param {string} template Path of the template file being rendered, relative to views/
* @param {object} data Object of data to pass to the template
*/
async render(template, data) {
let rendered;
try {
rendered = this.engine.compileFile(path.join(this.viewsPath, template));
} catch (error) {
return new SaplingError(error);
}
return rendered(_.extend(data, this.tags));
}
/**
* Register custom tags with the template engine
*
* @param {object} tags Object of functions
*/
async registerTags(tags) {
this.tags = tags;
}
}