浏览器使用art-template
引入js
安装
npm install art-template --save
在需要的文件模块中引用 art-template
<script src="./node_modules/art-template/lib/template-web.js"></script>
或者下载直接下载 引入
查看文档 api
完整代码 + 效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浏览器使用art-template.html</title>
<script src="./node_modules/art-template/lib/template-web.js"></script>
</head>
<body>
<span class="_span"></span>
<div class="div"></div>
<script type="text/template" id="tp1">
hello {{ name }} {{age}}
喜欢 {{ each hobbies}} {{ $value }} {{/each}} //遍历
</script>
<!-- type="text/html" 有html编写高亮提示-->
<script type="text/html" id="tp2">
<div>hello {{ name }} {{age}}</div>
<h1>喜欢 {{ each hobbies}} {{ $value }} {{/each}} //遍历</h1>
</script>
<script>
var ret = template('tp1',{
name:'ggggg',
age:18,
hobbies:[
'写代码',"看书","打游戏"
]
})
var _span = document.querySelector('._span')
_span.innerText = ret;
console.log(ret)
var ret2 = template('tp2',{
name:'ggggg',
age:18,
hobbies:[
'写代码',"看书","打游戏"
]
})
var div = document.querySelector('.div')
div.innerHTML = ret2 ;
</script>
</body>
</html>
效果图