art-template

2019-12-18  本文已影响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>
上一篇下一篇

猜你喜欢

热点阅读