您的位置:首页 > 其它

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

2016-06-20 23:33 246 查看
var html_line = html.replace(/[\r\t\n]+/g, "\n").split(/\n/)
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 += '\n return _h.join("")'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: