模板拼装最简单处理 字符串拼接版本

var html_line = html.replace(/[ ]+/g, " ").split(/ /)
var tpl = `var _h = []`
var _begin = '<?' , _end = '?>'
,_is = true
html_line.forEach(function(line){

while(true){
var _pos = line.indexOf(_is ? _begin : _end)
if (_pos !== -1){
var _part = line.slice(0 ,_pos )

line = line.slice(_pos + 2 )
if (_is) {
_part = _part.replace(/"/g,`\"`)
if (_part.length) {
tpl += ` _h.push( "${_part}");`
}
} else {
if ('=' == _part.slice(0,1)) {
_part = _part.slice(1)
tpl += ` _h.push( ${_part});`
}else{
tpl += ` ${_part}`
}
}

}else {
if (_is) {
line = line.replace(/"/g,`\"`)
if (line.length) {
tpl += ` _h.push( "${line}");`
}
} else {
tpl += ` ${line}`
}
break
}
_is = !_is
}

})
tpl += ' return _h.join("")'

原文地址:https://www.cnblogs.com/vaal-water/p/5602201.html