let template = 'hello {{obj.myName}} {{obj.age}}!'
let obj = { myName: 'bll', age: 10 }
function compile(template) {
let html = template.replace(/\{\{([\s\S]+?)\}\}/g, (match, code) => {

return `' + ${code} + '`
})
html = `let str = '${html}'; return str`
console.log('html', html)
return new Function('obj', html)
}
let str = compile(template)
console.log(str(obj)) // hello world
let template = 'hello <% name %>!'
let name1 = 'world'
function compile(template) {
let html = template.replace(/<%([\s\S]+?)%>/g, (match, code) => {

return `' + ${code} + '`
})
html = `let str = '${html}'; return str`
return new Function('name', html)
}
let str = compile(template)
console.log(str(name1)) // hello world!

模板引擎小demo_html