模板引擎概述
作用:使用模板引擎提供的模板语法,可以将数据和 HTML 拼接起来。
官方地址: https://aui.github.io/art-template/zh-cn/index.html
安装 npm install art-template --save (这是用在服务器端的)
而在浏览器中实时编译
需要下载:template-web.js(gzip: 6kb)
步骤
-
下载 art-template 模板引擎库文件并在 HTML 页面中引入库文件
-
准备 art-template 模板 通过id
<script id="tpl" type="text/html">
<div class="box"></div>
</script>
- 告诉模板引擎将哪一个模板和哪个数据进行拼接
var html = template(‘tpl’, {username: ‘zhangsan’, age: ‘20’}); - 将拼接好的html字符串添加到页面中
document.getElementById(‘container’).innerHTML = html; - 通过模板语法告诉模板引擎,数据和html字符串要如何拼接
<script id="tpl" type="text/html">
<div class="box"> {
{ username }} </div>
</script>