-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplateEngine.js
32 lines (25 loc) · 933 Bytes
/
templateEngine.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
/*
A lightweight template engine for Node.js that supports
- dynamic HTML rendering using a custom **Lodash Template** build
- asynchronous file handling with **fs.promises**
- support for partial templates through the `{% include %}` syntax.
*/
const fs = require('fs').promises;
const _ = require('./custom_modules/lodash.custom.js');
async function includePartial(baseView) {
const reg = /{%\s+include?\s+(.*?)\s+%}/mg;
let template = baseView;
while(true) {
const match = reg.exec(template);
if (!match) return template;
const partial = match[1]
let partialContent = await fs.readFile(`./views/${partial}.html`, 'utf-8');
template = template.replace(match[0], partialContent)
}
}
async function render(view, ctx = {}) {
let content = await fs.readFile(`./views/${view}.html`, 'utf-8');
content = await includePartial(content)
return _.template(content)(ctx);
}
module.exports = { render }