-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
29 lines (24 loc) · 819 Bytes
/
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
'use strict'
const path = require('path')
const { include, renderTemplate } = require('./render')
function renderWith (content, values, cb) {
if (typeof values === 'function') [cb, values] = [values, {}]
return (renderer) => {
let promise
if (!cb) {
promise = new Promise((resolve, reject) => {
cb = (err, result) => err ? reject(err) : resolve(result)
})
}
renderer(content, values).then(data => cb(null, data)).catch(cb)
return promise
}
}
function render (content, values = {}, cb) {
return renderWith(content, values, cb)(renderTemplate)
}
function renderFile (file, values = {}, cb) {
file = path.isAbsolute(file) ? file : path.join(__dirname, file)
return renderWith(file, values, cb)(include(path.dirname(file)))
}
module.exports = { render, renderFile }