art-template的使用步骤

1.导入art-template

2.定义数据

3.定义模板

4.调用template函数

5.渲染HTML结构 

代码示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1.导入模板引擎 -->
    <!-- 在window全局 多一个函数叫做template('模板的ID','需要渲染的数据对象') -->
    <script src="/js文件/template-web.js"></script>
</head>

<body>

</body>
<div id="container"></div>
<!-- 3.定义模板 -->
<!-- 3.1模板的HTML必须定义到script标签中 -->
<script type="text/html" id="tpl-user">
    <h1>{{name}} ------- {{age}}</h1>
</script>


<script>
    // 2.定义需要渲染的数据
    var data = {
        name: 'zs',
        age: 20
    }
    // 4.调用template函数
    var htmlStr = template('tpl-user', data)
    console.log(htmlStr);
    // 5.渲染HTML结构
    $('#container').html(htmlStr)
</script>

</html>

 

posted @ 2022-04-29 13:11  今天穿秋裤了吗  阅读(356)  评论(0)    收藏  举报