Skip to content

Instantly share code, notes, and snippets.

@Cyang39
Last active April 21, 2020 07:13
Show Gist options
  • Save Cyang39/7ad13db01016e5d22605976070237334 to your computer and use it in GitHub Desktop.
Save Cyang39/7ad13db01016e5d22605976070237334 to your computer and use it in GitHub Desktop.
Use loadsh _.template() instead of using ejs template engine.
const _ = require("lodash")
const fs = require("fs")
const map = {}
function readTemplate(path) {
if (!map[path]) {
const str = _(fs.readFileSync(path, "utf-8"))
.replace(/<%-/g, "TEMP<%-TEMP")
.replace(/<%=/g, "<%-")
.replace(/TEMP<%-TEMP/g, "<%=")
map[path] = _.template(str)
}
return map[path]
}
function include(name, passin = {}) {
const _include = (name, passin) =>
readTemplate(`templates/${name}.template.html`)(passin)
passin.include = _include
return _include(name, passin)
}
function renderFile(path, passin = {}) {
passin.include = include
return readTemplate(path)(passin)
}
module.exports = renderFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment