使用方法:
1、引入模板引擎 ;
2、写模板;
3、映射数据
4、写入html数据
展示如下:
整体代码展示如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="../js/template-web.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const student = {
stuList: [
{
name: '刘亦菲',
age: 18,
adrr: "纽约"
},
{
name: '杨紫',
age: 16,
adrr: "北京"
}
]
}
</script>
<script id="popList" type="text/html">
{{each stuList as item}}
<ul>
<li> {{item.name}}</li>
<li> {{item.age}}</li>
<li> {{item.adrr}}</li>
</ul>
{{/each}}
</script>
<script>
window.onload = function () {
const stuTemplate = template('popList', student);
document.getElementById('content').innerHTML = stuTemplate;
}
</script>
<div id="content"></div>
</body>
</html>
存放模板引擎目录展示: