激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務(wù)器之家 - 編程語(yǔ)言 - JavaScript - 基于jQuery的JavaScript模版引擎JsRender使用指南

基于jQuery的JavaScript模版引擎JsRender使用指南

2021-06-09 17:15jQuery教程網(wǎng) JavaScript

這篇文章主要介紹了基于jQuery的JavaScript模版引擎JsRender使用指南,需要的朋友可以參考下

前言

     JsRender是一款基于jQuery的JavaScript模版引擎,它具有如下特點(diǎn):

          ·  簡(jiǎn)單直觀

          ·  功能強(qiáng)大

          ·  可擴(kuò)展的

          ·  快如閃電

     這些特性看起來很厲害,但幾乎每個(gè)模版引擎,都會(huì)這么宣傳。。。

     由于工作需要,小菜才接觸到此款模版引擎。使用了一段時(shí)間,發(fā)現(xiàn)它確實(shí)比較強(qiáng)大,但小菜覺得有些地方強(qiáng)大的過頭了,反倒讓人覺得很難理解。

     另一方面,JsRender的官方文檔比較詳細(xì),但其他資料出奇的少,遇到點(diǎn)什么問題,基本搜不到,不僅僅是相關(guān)問題搜不到,幾乎就是沒有結(jié)果。

     再加上JsRender有些地方確實(shí)是不好理解,所以急需小菜分享一些“最佳實(shí)踐”。

     基于最近一段時(shí)間的使用,小菜總結(jié)了一些實(shí)用經(jīng)驗(yàn),當(dāng)然,這些經(jīng)驗(yàn)在官方文檔上是找不到的。

     注意:本文不是基礎(chǔ)入門教程,以下例子中自帶注釋,不做過多說明,讀者自行體會(huì),不懂的地方可以留言。

 嵌套循環(huán)使用#parent訪問父級(jí)數(shù)據(jù)(不推薦)

 

復(fù)制代碼 代碼如下:

 <!DOCTYPE html>
 <html>
   <head>
     <meta charset="utf-8">
     <title>嵌套循環(huán)使用#parent訪問父級(jí)數(shù)據(jù) --- by 楊元</title>
     <style>
     </style>
   </head>
   <body>
     <div>
       <table>
         <thead>
           <tr>
             <th width="10%">序號(hào)</th>
             <th width="10%">姓名</th>
             <th width="80%">家庭成員</th>
           </tr>
         </thead>
         <tbody id="list">
         </tbody>
       </table>
     </div>
     <script src="jquery.min.js"></script>
     <script src="jsviews.js"></script>
     <!-- 定義JsRender模版 -->
     <script id="testTmpl" type="text/x-jsrender">
       <tr>
         <td>{{:#index + 1}}</td>
         <td>{{:name}}</td>
         <td>
           {{for family}}
             {{!-- 利用#parent訪問父級(jí)index --}}
             <b>{{:#parent.parent.index + 1}}.{{:#index + 1}}</b>
             {{!-- 利用#parent訪問父級(jí)數(shù)據(jù),父級(jí)數(shù)據(jù)保存在data屬性中 --}}
             {{!-- #data相當(dāng)于this --}}
             {{:#parent.parent.data.name}}的{{:#data}}
           {{/for}}
         </td>
       </tr>
     </script>
     <script>
       //數(shù)據(jù)源
       var dataSrouce = [{
         name: "張三",
         family: [
           "爸爸",
           "媽媽",
           "哥哥"
         ]
       },{
         name: "李四",
         family: [
           "爺爺",
           "奶奶",
           "叔叔"
         ]
       }];
       //渲染數(shù)據(jù)
       var html = $("#testTmpl").render(dataSrouce);
       $("#list").append(html);
     </script>
   </body>
 </html>

 

嵌套循環(huán)使用參數(shù)訪問父級(jí)數(shù)據(jù)(推薦)

 

復(fù)制代碼 代碼如下:

 <!DOCTYPE html>
 <html>
   <head>
     <meta charset="utf-8">
     <title>嵌套循環(huán)使用參數(shù)訪問父級(jí)數(shù)據(jù) --- by 楊元</title>
     <style>
     </style>
   </head>
   <body>
     <div>
       <table>
         <thead>
           <tr>
             <th width="10%">序號(hào)</th>
             <th width="10%">姓名</th>
             <th width="80%">家庭成員</th>
           </tr>
         </thead>
         <tbody id="list">
         </tbody>
       </table>
     </div>
     <script src="jquery.min.js"></script>
     <script src="jsviews.js"></script>
     <!-- 定義JsRender模版 -->
     <script id="testTmpl" type="text/x-jsrender">
       <tr>
         <td>{{:#index + 1}}</td>
         <td>{{:name}}</td>
         <td>
           {{!-- 使用for循環(huán)時(shí),可以在后邊添加參數(shù),參數(shù)必須以~開頭,多個(gè)參數(shù)用空格分隔 --}}
           {{!-- 通過參數(shù),我們緩存了父級(jí)的數(shù)據(jù),在子循環(huán)中通過訪問參數(shù),就可以間接訪問父級(jí)數(shù)據(jù) --}}
           {{for family ~parentIndex=#index ~parentName=name}}
             <b>{{:~parentIndex + 1}}.{{:#index + 1}}</b>
             {{!-- #data相當(dāng)于this --}}
             {{:~parentName}}的{{:#data}}
           {{/for}}
         </td>
       </tr>
     </script>
     <script>
       //數(shù)據(jù)源
       var dataSrouce = [{
         name: "張三",
         family: [
           "爸爸",
           "媽媽",
           "哥哥"
         ]
       },{
         name: "李四",
         family: [
           "爺爺",
           "奶奶",
           "叔叔"
         ]
       }];
       //渲染數(shù)據(jù)
       var html = $("#testTmpl").render(dataSrouce);
       $("#list").append(html);
     </script>
   </body>
 </html>

 

 自定義標(biāo)簽(custom tag)中使用else(強(qiáng)烈不推薦)

 

復(fù)制代碼 代碼如下:

 <!DOCTYPE html>
 <html>
   <head>
     <meta charset="utf-8">
     <title>自定義標(biāo)簽中使用else --- by 楊元</title>
     <style>
     </style>
   </head>
   <body>
     <div>
       <table>
         <thead>
           <tr>
             <th width="50%">名稱</th>
             <th width="50%">單價(jià)</th>
           </tr>
         </thead>
         <tbody id="list">
         </tbody>
       </table>
     </div>
     <script src="jquery.min.js"></script>
     <script src="jsviews.js"></script>
     <!-- 定義JsRender模版 -->
     <script id="testTmpl" type="text/x-jsrender">
       <tr>
         <td>{{:name}}</td>
         <td>
           {{!-- isShow為自定義標(biāo)簽,price是傳入的參數(shù),status是附加屬性 --}}
           {{isShow price status=0}}
             {{:price}}
           {{else price status=1}}
             --
           {{/isShow}}
         </td>
       </tr>
     </script>
     <script>
       //數(shù)據(jù)源
       var dataSrouce = [{
         name: "蘋果",
         price: 108
       },{
         name: "鴨梨",
         price: 370
       },{
         name: "桃子",
         price: 99
       },{
         name: "菠蘿",
         price: 371
       },{
         name: "橘子",
         price: 153
       }];
       //自定義標(biāo)簽
       $.views.tags({
         "isShow": function(price){
           var temp=price+''.split('');
           if(this.tagCtx.props.status === 0){
             //判斷價(jià)格是否為水仙花數(shù),如果是,則顯示,否則不顯示
             if(price==(Math.pow(parseInt(temp[0],10),3)+Math.pow(parseInt(temp[1],10),3)+Math.pow(parseInt(temp[2],10),3))){
               return this.tagCtxs[0].render();
             }else{
               return this.tagCtxs[1].render();
             }
           }else{
             return "";
           }
         }
       });
       //渲染數(shù)據(jù)
       var html = $("#testTmpl").render(dataSrouce);
       $("#list").append(html);
     </script>
   </body>
 </html>

 

用helper代替自定義標(biāo)簽(推薦)

 

復(fù)制代碼 代碼如下:

 <!DOCTYPE html>
 <html>
   <head>
     <meta charset="utf-8">
     <title>用helper代替自定義標(biāo)簽 --- by 楊元</title>
     <style>
     </style>
   </head>
   <body>
     <div>
       <table>
         <thead>
           <tr>
             <th width="50%">名稱</th>
             <th width="50%">單價(jià)</th>
           </tr>
         </thead>
         <tbody id="list">
         </tbody>
       </table>
     </div>
     <script src="jquery.min.js"></script>
     <script src="jsviews.js"></script>
     <!-- 定義JsRender模版 -->
     <script id="testTmpl" type="text/x-jsrender">
       <tr>
         <td>{{:name}}</td>
         <td>
           {{!-- 利用原生的if做分支跳轉(zhuǎn),利用helper做邏輯處理 --}}
           {{if ~isShow(price)}}
             {{:price}}
           {{else}}
             --
           {{/if}}
         </td>
       </tr>
     </script>
     <script>
       //數(shù)據(jù)源
       var dataSrouce = [{
         name: "蘋果",
         price: 108
       },{
         name: "鴨梨",
         price: 370
       },{
         name: "桃子",
         price: 99
       },{
         name: "菠蘿",
         price: 371
       },{
         name: "橘子",
         price: 153
       }];
       //Helper
       $.views.helpers({
         "isShow": function(price){
           var temp=price+''.split('');
           if(price==(Math.pow(parseInt(temp[0],10),3)+Math.pow(parseInt(temp[1],10),3)+Math.pow(parseInt(temp[2],10),3))){
             return true;
           }else{
             return false;
           }
         }
       });
       //渲染數(shù)據(jù)
       var html = $("#testTmpl").render(dataSrouce);
       $("#list").append(html);
     </script>
   </body>
 </html>

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中文在线观看视频 | 午夜精品福利影院 | 国产一区二区三区视频在线 | 天堂成人一区二区三区 | 中文字幕网站在线 | 日日摸夜夜骑 | 亚洲自拍第二页 | 日韩字幕在线观看 | 午夜在线视频一区二区三区 | 欧美一级黄 | 久久久国产一级片 | 午夜爽爽爽男女免费观看hd | 欧美黄色大片免费观看 | 欧美一级免费在线观看 | 99在线啪 | 免费毛片在线 | 青草久久网| 中文欧美日韩 | 欧美一级淫片a免费播放口 九九视频精品在线 | h色视频在线观看 | 国产成人高潮免费观看精品 | 色网站免费观看 | 欧美三级美国一级 | 91不雅视频 | 日本高清com | 在线播放黄色片 | 欧美aaa | 欧美爱爱视频网站 | 1区2区3区在线观看 欧美特黄a | 男女牲高爱潮免费视频男女 | 嗯哈~不行好大h双性 | 日本久久网站 | 欧美成人亚洲 | 黄色欧美精品 | 日本特级a一片免费观看 | 日韩色电影 | 麻豆传传媒久久久爱 | 成年人黄视频 | 色综合欧美 | www.成人免费视频 | 久久蜜桃香蕉精品一区二区三区 |