art-template渲染真实数据--后台接口(难度:3颗星)

<!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">
    <script src="./lib/js/jquery.min.js"></script>
    <script src="./lib/js/art-template.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="middle">
        <!-- 在里面渲染真实数据 -->
    </div>
    <script type="text/html" id="article-detail">
        {{each articles value index}}
        <h3 class="title">{{value.title}}</h3>
        <p class="content">{{#value.content}}</p>
        {{/each}}
    </script>
    <script>
        $.ajax({
            type:'get',
            url:'https://wwwxxxxxxxxxxxx',
            data:{
                    xxxxxxxxxxxx
            },
            dataType:'JSON',
            success:res => {
                console.log(res);
                const articles=res.Data[0];
                console.log(articles);
                const htmlStr=template('article-detail',articles);
                $('#middle').html(htmlStr);
            },
            error:msg=>{
                console.log(msg);
            }
        });
    </script>
</body>
</html>
 
 
与下面实现功能一样
 
 
<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./lib/css/bootstrap-main.css">
    <script src="./lib/js/jquery.min.js"></script>
    <script src="./lib/js/art-template.js"></script>
    <title>art-template渲染</title>
</head>

<body>
    <div class="col-sm-11" id="middle">
    </div>
    <script type="text/html" id="article-detail">
        <h3 class="title">{{title}}</h3>
        <h5 class="editor">作者:{{nickName}} &nbsp;发布于:<span class="time">{{time.substring(0,10)}}</span> &nbsp;阅读数:<span
                class="read-number">{{viewCount}}</span></h5>
        <div class="detail">
            <p>{{#content}}</p>
        </div>
        <span>喜欢:{{likeCount}}</span>
    </script>

    <script>
        $.ajax({
            type: 'get',
            url: "https://www.xxxxxxxapi/article?articleId=1",
            dataType: 'JSON',
            success: res => {
                // console.log(res);
                var article = res.Data[0].article;
                console.log(article);
                const htmlStr = template('article-detail', {
                    title: article.title,
                    nickName: article.nickName,
                    viewCount: article.viewCount,
                    content: article.content,
                    time: article.publishTime,
                    likeCount: article.likeCount
                });
                $('#middle').html(htmlStr);
            },
            error: msg => {
                console.log(msg);
            }
        })
    </script>
</body>

</html>
posted @ 2020-04-03 09:29  我和我的小生活  阅读(327)  评论(0)    收藏  举报