美文网首页
art-template

art-template

作者: 王哈哈就很棒 | 来源:发表于2019-12-18 14:47 被阅读0次

列表渲染

<!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>Document</title>
</head>
<body>
    <div id="container"></div>
    
    <script src="https://unpkg.com/art-template@4.13.2/lib/template-web.js"></script>
    <script id="user-tpl" type="text/html">
        
        <table border="1" width="80%" align="center">
            <tr>
                <th>id</th>
                <th>name</th>
                <th>age</th>
            </tr>
            {{each users}}
                <tr>
                    <td>{{$value.id}}</td>
                    <td>{{$value.name}}</td>
                    <td>{{$value.age}}</td>
                </tr>
            {{/each}}
        </table>
    </script>

    <script>
        var users = [
            {'id': 1, name: 'zhangsan', age: 21},
            {'id': 2, name: 'lisi', age: 22},
            {'id': 3, name: 'wangwu', age: 23}
        ]

        var html = template('user-tpl', {users: users})
        var container = document.querySelector('#container')
        container.innerHTML = html;
    </script>
</body>
</html>

条件渲染

<!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>Document</title>
</head>
<body>
    <div id="container"></div>

    <script id="tpl" type="text/html">
        <!--if else 判断-->
        {{if user == "admin"}}
            <p>welcome {{user}}</p>
        {{else}}
            <p>no user</p>
        {{/if}}
    </script>
    <script src="https://unpkg.com/art-template@4.13.2/lib/template-web.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script>
        var user = "admin"
        var html = template("tpl", {user: user})
        var container = document.querySelector("#container")
        container.innerHTML = html;
    </script>
</body>
</html>

相关文章

网友评论

      本文标题:art-template

      本文链接:https://www.haomeiwen.com/subject/cylunctx.html