欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript構(gòu)建自己的模板小引擎示例

 更新時(shí)間:2018年12月14日 10:57:50   作者:湯姆大叔  
這篇文章主要介紹了JavaScript構(gòu)建自己的模板小引擎,結(jié)合實(shí)例形式分析了javascript自定義模板引擎的實(shí)現(xiàn)與使用方法,需要的朋友可以參考下

本文實(shí)例講述了JavaScript構(gòu)建自己的模板小引擎。分享給大家供大家參考,具體如下:

有時(shí)候,我們不需要太牛逼太強(qiáng)大的JavaScript模板引擎(比如jQuery tmpl或者h(yuǎn)andlebarsjs),我們只是需要在簡(jiǎn)單的模板里綁定一些非常簡(jiǎn)單的字段,本文將使用非常簡(jiǎn)單的技巧來(lái)幫你實(shí)現(xiàn)這個(gè)小功能。

首先我們先來(lái)定義我們需要的模板,在id為template的script塊里:

HTML部分:

<!doctype html>
<html>
<head>
  <meta charset=utf-8>
  <title>Simple Templating</title>
</head>
<body>
 <div class="result"></div>
 <script type="template" id="template">
  <h2>
   <a href="{{href}}" rel="external nofollow" >
    {{title}}
   </a>
  </h2>
  <img src="{{imgSrc}}" alt="{{title}}">
 </script>
</body>
</html>

css樣式:

a:link, a:visited {
  color: #3D81EE;
}

然后,我們需要通過(guò)Ajax等其它方式獲取所需要的數(shù)據(jù),這里為了展示方便,我們使用了自己定義的數(shù)組:

var data = [
  {
   title: "HTML5+SVG實(shí)現(xiàn)的圣誕夜棒棒糖山林雪景動(dòng)畫(huà)效果",
   href: "http://www.dbjr.com.cn/jiaoben/649311.html",
   imgSrc: "http://img.jbzj.com/do/uploads/litimg/181205/162543361311.jpg"
  },
  {
   title: "微信小程序?qū)崙?zhàn)入門(mén)(內(nèi)含完整實(shí)例解析) 劉明洋著",
   href: "http://www.dbjr.com.cn/books/648114.html",
   imgSrc: "http://img.jbzj.com/do/uploads/litimg/181128/1H13HM103.jpg"
  },
  {
   title: "JavaScript開(kāi)發(fā)框架權(quán)威指南",
   href: "http://www.dbjr.com.cn/books/636104.html",
   imgSrc: "http://img.jbzj.com/do/uploads/litimg/180910/1H9462K325.jpg"
  }
],

我們有2種方式來(lái)綁定這些數(shù)據(jù)到模板上,第一種是非常簡(jiǎn)單的hardcode方法,第二種是自動(dòng)識(shí)別變量式的。

我們先來(lái)看第一種方式,是通過(guò)替換花括號(hào)里的值為data里所對(duì)應(yīng)的值來(lái)達(dá)到目的:

template = document.querySelector('#template').innerHTML,
result = document.querySelector('.result'),
i = 0, len = data.length,
fragment = '';
for ( ; i < len; i++ ) {
  fragment += template
   .replace( /\{\{title\}\}/, data[i].title )
   .replace( /\{\{href\}\}/, data[i].href )
   .replace( /\{\{imgSrc\}\}/, data[i].imgSrc );
}
result.innerHTML = fragment;

完整js部分:

;(function() {
 // simulates AJAX request
 var data = [
  {
   title: "HTML5+SVG實(shí)現(xiàn)的圣誕夜棒棒糖山林雪景動(dòng)畫(huà)效果",
   href: "http://www.dbjr.com.cn/jiaoben/649311.html",
   imgSrc: "http://img.jbzj.com/do/uploads/litimg/181205/162543361311.jpg"
  },
  {
   title: "微信小程序?qū)崙?zhàn)入門(mén)(內(nèi)含完整實(shí)例解析) 劉明洋著",
   href: "http://www.dbjr.com.cn/books/648114.html",
   imgSrc: "http://img.jbzj.com/do/uploads/litimg/181128/1H13HM103.jpg"
  },
  {
   title: "JavaScript開(kāi)發(fā)框架權(quán)威指南",
   href: "http://www.dbjr.com.cn/books/636104.html",
   imgSrc: "http://img.jbzj.com/do/uploads/litimg/180910/1H9462K325.jpg"
  }
 ],
   template = document.querySelector('#template').innerHTML,
   result = document.querySelector('.result'),
   i = 0, len = data.length,
   fragment = '';
 for ( ; i < len; i++ ) {
  fragment += template
   .replace( /\{\{title\}\}/, data[i].title )
   .replace( /\{\{href\}\}/, data[i].href )
   .replace( /\{\{imgSrc\}\}/, data[i].imgSrc );
 }
 result.innerHTML = fragment;
})();

這里使用在線(xiàn)HTML/CSS/JavaScript前端代碼調(diào)試運(yùn)行工具http://tools.jb51.net/code/WebCodeRun 測(cè)試上述代碼運(yùn)行效果:

第二種方式比較靈活,是通過(guò)正則表達(dá)式來(lái)替換所有花括號(hào)的值,而無(wú)需一個(gè)一個(gè)替換,這樣相對(duì)來(lái)說(shuō)比較靈活,但是要注意模板標(biāo)簽可能在數(shù)據(jù)里不存在的情況:

template = document.querySelector('#template').innerHTML,
result = document.querySelector('.result'),
attachTemplateToData;
// 將模板和數(shù)據(jù)作為參數(shù),通過(guò)數(shù)據(jù)里所有的項(xiàng)將值替換到模板的標(biāo)簽上(注意不是遍歷模板標(biāo)簽,因?yàn)闃?biāo)簽可能不在數(shù)據(jù)里存在)。
attachTemplateToData = function(template, data) {
    var i = 0,
      len = data.length,
      fragment = '';
    // 遍歷數(shù)據(jù)集合里的每一個(gè)項(xiàng),做相應(yīng)的替換
    function replace(obj) {
      var t, key, reg;
       //遍歷該數(shù)據(jù)項(xiàng)下所有的屬性,將該屬性作為key值來(lái)查找標(biāo)簽,然后替換
      for (key in obj) {
        reg = new RegExp('{{' + key + '}}', 'ig');
        t = (t || template).replace(reg, obj[key]);
      }
      return t;
    }
    for (; i < len; i++) {
      fragment += replace(data[i]);
    }
    return fragment;
  };
result.innerHTML = attachTemplateToData(template, data);

此時(shí)js部分完整代碼:

;(function() {
 // simulates AJAX request
 var data = [
  {
   title: "HTML5+SVG實(shí)現(xiàn)的圣誕夜棒棒糖山林雪景動(dòng)畫(huà)效果",
   href: "http://www.dbjr.com.cn/jiaoben/649311.html",
   imgSrc: "http://img.jbzj.com/do/uploads/litimg/181205/162543361311.jpg"
  },
  {
   title: "微信小程序?qū)崙?zhàn)入門(mén)(內(nèi)含完整實(shí)例解析) 劉明洋著",
   href: "http://www.dbjr.com.cn/books/648114.html",
   imgSrc: "http://img.jbzj.com/do/uploads/litimg/181128/1H13HM103.jpg"
  },
  {
   title: "JavaScript開(kāi)發(fā)框架權(quán)威指南",
   href: "http://www.dbjr.com.cn/books/636104.html",
   imgSrc: "http://img.jbzj.com/do/uploads/litimg/180910/1H9462K325.jpg"
  }
 ],
    template = document.querySelector('#template').innerHTML,
    result = document.querySelector('.result'),
    attachTemplateToData;
  // 將模板和數(shù)據(jù)作為參數(shù),通過(guò)數(shù)據(jù)里所有的項(xiàng)將值替換到模板的標(biāo)簽上(注意不是遍歷模板標(biāo)簽,因?yàn)闃?biāo)簽可能不在數(shù)據(jù)里存在)。
  attachTemplateToData = function(template, data) {
    var i = 0,
      len = data.length,
      fragment = '';
    // 遍歷數(shù)據(jù)集合里的每一個(gè)項(xiàng),做相應(yīng)的替換
    function replace(obj) {
      var t, key, reg;
      for (key in obj) {
        reg = new RegExp('{{' + key + '}}', 'ig');
        t = (t || template).replace(reg, obj[key]);
      }
      return t;
    }
    for (; i < len; i++) {
      fragment += replace(data[i]);
    }
    return fragment;
  };
  result.innerHTML = attachTemplateToData(template, data);
})();

感興趣的朋友可以使用在線(xiàn)HTML/CSS/JavaScript前端代碼調(diào)試運(yùn)行工具http://tools.jb51.net/code/WebCodeRun 測(cè)試上述代碼運(yùn)行效果。

這樣,我們就可以做到,無(wú)限制定義自己的標(biāo)簽和item屬性了,而無(wú)需修改JS代碼。

更多關(guān)于模板引擎的信息,你可以訪(fǎng)問(wèn)如下2個(gè)地址,這2個(gè)引擎都不錯(cuò)哦。

HandleBars.js
Mustache.js

參考原文:http://net.tutsplus.com/tutorials/javascript-ajax/create-a-makeshift-javascript-templating-solution/

更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專(zhuān)題:《javascript面向?qū)ο笕腴T(mén)教程》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論