Last active
April 21, 2020 07:13
-
-
Save Cyang39/7ad13db01016e5d22605976070237334 to your computer and use it in GitHub Desktop.
Use loadsh _.template() instead of using ejs template engine.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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