• 模板引擎小结-使用


    发送网络请求将服务器返回的数据进行渲染

              let showData = []

              $.each(dataArr,function (i,item) {

                showData.push('

    '+item.username+'河南-郑州'+item.content+'
    '+item.time+'
    ')

              })

              $('.comments').empty().html(showData)

    上述代码是通过字符串拼接的形式来渲染UI结构

    如果UI结构比较负载,则拼接字符串的时候要格外注意引号之前的嵌套。且一旦需求发生变化修改起来也非常麻烦

    如果分离开操作也是比较麻烦的

    要重复的获取元素 进行内部填充

    1. <h3 class="title">h3>
    2. <div>
    3. 姓名:<span id="name">span>
    4. div>
    5. <div>
    6. 年龄:<span id="age">span>
    7. div>
    8. <div>
    9. 是否会员:<span id="isVIP">span>
    10. div>
    11. <div>
    12. 注册时间:<span id="firstTime">span>
    13. div>
    14. <div>
    15. <ul class="hobby">
    16. ul>
    17. div>
    18. <script>
    19. /**传统渲染数据 操作太多
    20. **/
    21. const data = {
    22. title:'传统渲染',
    23. name:'马克李',
    24. age:24,
    25. isVIP:'否',
    26. firstTime:'2022-05-09',
    27. hobby:['吃饭','睡觉','打豆豆']
    28. }
    29. $('.title').html(data.title)
    30. $('#name').html(data.name)
    31. $('#age').html(data.age)
    32. $('#isVIP').html(data.isVIP)
    33. $('#firstTime').html(data.firstTime)
    34. $.each(data.hobby,function (i,item) {
    35. $('.hobby').append('
    36. '+item+'
    37. ')
  • })
  • script>
  • 所以我们使用模板引擎

    ① 减少了字符串拼接的操作

    ② 使代码结构更清晰

    ③ 使代码更易于阅读与维护

    使用步骤

    ① 导入 art-template

     

    ② 定义需要渲染的数据

      let data = {

        name:'巴达木',

        age:35,

        birthday:1987,

        start:'

    123

    ',

        flag:0,

        hobby:[

          'HTML',

          'CSS',

          'JavaScript',

          'jQuery',

          'Ajax',

          'XML',

          'Vue',

        ],

        regTime:new Date()

      }

    ③ 定义模板 定义到script 标签中的type = text/html

    ④ 调用template函数

    let htmlStr = template('tpl',data)

    tpl 是script模板的id

    data 是要渲染的数据

    返回的是html 字符串

    ⑤ 把数据 渲染到页面当中

      $('#container').html(htmlStr) 

     

    介绍下第三步 定义模板中的语法规则

    art-template 提供了 {{}} 这种语法格式 在{{}}内可以进行变量输出。或循环数组等操作。这种{{}}语法在art-template中被称为标准语法。

    在{{}} 中可以及进行 变量输出 对象属性输出 三元表达式输出 加减乘除表达式输出 

     还可以进行条件输出

      {{if flag === 0}}

        flag的值是0

        {{else if flag === 1}}

        flag的值是1

        {{/if}}

    循环输出

    {{}} 内通过each 语法循环数组,当前循环的索引使用 $index进行访问 当前的循环项使用$value进行访问。

        {{each hobby}}

       

  • 学习步骤是:{{$index}}、将会依次掌握 {{$value}}
  •     {{/each}}

    还可以定义过滤器

    template.defaults.imports.filterName = function(value){/*return 处理的结果*/}

      // 定义处理时间的过滤器

      template.defaults.imports.dateSet = function(date){

        let y = date.getFullYear()

        let m = date.getMonth() +1

        let d = date.getDate()

        return y + '-' +m +'-'+ d

      }

    新闻列表的渲染案例

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>09新闻列表案例title>
    6. <script src="http://www.wsg3096.com/ass/jquery-3.6.0.js">script>
    7. <link rel="stylesheet" href="http://www.wsg3096.com/ass/initialize.css">
    8. <script src="pic/template-web.js">script>
    9. <style>
    10. .newsny-box{width: 930px;
    11. padding: 20px 0;
    12. border-bottom: dashed 1px #dbdbdb;
    13. height: 140px;
    14. box-sizing: content-box;
    15. margin: 0 auto}
    16. .newsny01{width: 180px;
    17. height: 140px;}
    18. .newsny01 img{width: 215px;
    19. height: 140px;
    20. vertical-align: middle;}
    21. .newsny02{width: 690px;
    22. height: 140px;
    23. position: relative;}
    24. .newsny02 h3{font-size: 18px;
    25. color: #333;
    26. margin-top: 0px;
    27. margin-bottom: 10px;}
    28. .newsny02 h3 a{color: #333;}
    29. .newsny02 h3 a:hover{color: #147ce6;}
    30. .span{font-size: 14px;
    31. color: #999;
    32. position: absolute;
    33. bottom:0;
    34. right: 0px;}
    35. .i{font-size: 14px;
    36. color: #999;
    37. position: absolute;
    38. bottom:0;
    39. left: 0px;}
    40. .tab span{
    41. display: inline-block;
    42. margin: 0px 6px;
    43. padding: 4px 12px;
    44. border-radius: 8px;
    45. background: #e5e5f5;
    46. }
    47. .source{
    48. font-size: 15px;
    49. margin: 4px 0px 12px;
    50. }
    51. style>
    52. head>
    53. <body>
    54. <img src="pic/09.png" alt="" style="display: block">
    55. <div id="newBox">div>
    56. <script type="text/html" id="news">
    57. {{each data}}
    58. class="newsny-box">
    59. <div class="newsny01 fl">
    60. <a href="#"><img src="{{'http://www.liulongbin.top:3006'+$value.img}}">a>
    61. div>
    62. <div class="newsny02 fr">
    63. <h3><a href="#">{{$value.title}}a>h3>
    64. <p class="source">来源:{{$value.source}}p>
    65. <p class="tab">
    66. {{each $value.tags}}
    67. <span>{{$value}}span>
    68. {{/each}}
    69. p>
    70. <span class="span">【{{$value.time|dateSet}}】span>
    71. <i class="i">【浏览量:{{$value.cmtcount}}】i>
    72. div>
    73. <div class="cl">div>
    74. div>
    75. {{/each}}
    76. script>
    77. <script>
    78. $(function () {
    79. // function more10(...args){
    80. // $.each(args,function (i,ele) {
    81. // ele = ele<10 ? '0'+ele : ele
    82. // console.log(ele)
    83. // return ele
    84. // })
    85. // }
    86. function Zero(x){
    87. // if (x<10){
    88. // return '0'+x
    89. // }else {
    90. // return x
    91. // }
    92. return x = x<10 ? '0'+x : x
    93. }
    94. template.defaults.imports.dateSet = function(date){
    95. // 注意 这里是字符串 要new Date 一下
    96. let dt = new Date(date)
    97. let y = dt.getFullYear()
    98. let m = Zero(dt.getMonth() +1)
    99. let d = dt.getDate()
    100. let w = dt.getDay()
    101. let h = Zero(dt.getHours())
    102. let min = Zero(dt.getMinutes())
    103. let s = dt.getSeconds()
    104. let weeks = ['星期天','星期一','星期二','星期三','星期四','星期五','星期六']
    105. return y + "-" + m + "-" + d + weeks[w] + h + ":" + min + ":" + s
    106. }
    107. function getNew() {
    108. $.get(
    109. 'http://www.liulongbin.top:3006/api/news',
    110. function (res) {
    111. if (res.status !== 200){
    112. console.log('获取新闻失败')
    113. }
    114. $.each(res.data,function (i,ele) {
    115. // 把每一项的 tags 数组 转换为字符串
    116. ele.tags = ele.tags.split(',')
    117. })
    118. console.log(res)
    119. let htmlStr = template('news',res)
    120. $('#newBox').html(htmlStr)
    121. }
    122. )
    123. }
    124. getNew()
    125. })
    126. script>
    127. body>
    128. html>

  • 相关阅读:
    linux控制台命令
    Jetson Orin NX 开发指南(9): MAVROS 的安装与配置
    设计LRU缓存(双向链表+哈希表)
    UI自动化概念 + Web自动化测试框架介绍
    Python实战项目:打乒乓(源码分享)(文章较短,直接上代码)
    【每日练习】删除公共字符
    TreeMap排序探寻
    BCC源码内容概览(4)
    微信小程序如何设置一个全局的定时器,跳转页面也不清除,只有在某个条件改变的时候才清除
    OPENCV--调用GrabCut实现图像分割
  • 原文地址:https://blog.csdn.net/benlalagang/article/details/126135514