您好,登錄后才能下訂單哦!
本文實(shí)例講述了JavaScript構(gòu)建自己的模板小引擎。分享給大家供大家參考,具體如下:
有時(shí)候,我們不需要太牛逼太強(qiáng)大的JavaScript模板引擎(比如jQuery tmpl或者h(yuǎn)andlebarsjs),我們只是需要在簡(jiǎn)單的模板里綁定一些非常簡(jiǎn)單的字段,本文將使用非常簡(jiǎn)單的技巧來幫你實(shí)現(xiàn)這個(gè)小功能。
首先我們先來定義我們需要的模板,在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"> <h3> <a href="{{href}}" rel="external nofollow" > {{title}} </a> </h3> <img src="{{imgSrc}}" alt="{{title}}"> </script> </body> </html>
css樣式:
a:link, a:visited { color: #3D81EE; }
然后,我們需要通過Ajax等其它方式獲取所需要的數(shù)據(jù),這里為了展示方便,我們使用了自己定義的數(shù)組:
var data = [ { title: "HTML5+SVG實(shí)現(xiàn)的圣誕夜棒棒糖山林雪景動(dòng)畫效果", href: "https://www.jb51.net/jiaoben/649311.html", imgSrc: "http://img.jbzj.com/do/uploads/litimg/181205/162543361311.jpg" }, { title: "微信小程序?qū)崙?zhàn)入門(內(nèi)含完整實(shí)例解析) 劉明洋著", href: "https://www.jb51.net/books/648114.html", imgSrc: "http://img.jbzj.com/do/uploads/litimg/181128/1H13HM103.jpg" }, { title: "JavaScript開發(fā)框架權(quán)威指南", href: "https://www.jb51.net/books/636104.html", imgSrc: "http://img.jbzj.com/do/uploads/litimg/180910/1H9462K325.jpg" } ],
我們有2種方式來綁定這些數(shù)據(jù)到模板上,第一種是非常簡(jiǎn)單的hardcode方法,第二種是自動(dòng)識(shí)別變量式的。
我們先來看第一種方式,是通過替換花括號(hào)里的值為data里所對(duì)應(yīng)的值來達(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)畫效果", href: "https://www.jb51.net/jiaoben/649311.html", imgSrc: "http://img.jbzj.com/do/uploads/litimg/181205/162543361311.jpg" }, { title: "微信小程序?qū)崙?zhàn)入門(內(nèi)含完整實(shí)例解析) 劉明洋著", href: "https://www.jb51.net/books/648114.html", imgSrc: "http://img.jbzj.com/do/uploads/litimg/181128/1H13HM103.jpg" }, { title: "JavaScript開發(fā)框架權(quán)威指南", href: "https://www.jb51.net/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; })();
這里使用在線HTML/CSS/JavaScript前端代碼調(diào)試運(yùn)行工具:http://tools.jb51.net/code/WebCodeRun 測(cè)試上述代碼運(yùn)行效果:
第二種方式比較靈活,是通過正則表達(dá)式來替換所有花括號(hào)的值,而無需一個(gè)一個(gè)替換,這樣相對(duì)來說比較靈活,但是要注意模板標(biāo)簽可能在數(shù)據(jù)里不存在的情況:
template = document.querySelector('#template').innerHTML, result = document.querySelector('.result'), attachTemplateToData; // 將模板和數(shù)據(jù)作為參數(shù),通過數(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值來查找標(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)畫效果", href: "https://www.jb51.net/jiaoben/649311.html", imgSrc: "http://img.jbzj.com/do/uploads/litimg/181205/162543361311.jpg" }, { title: "微信小程序?qū)崙?zhàn)入門(內(nèi)含完整實(shí)例解析) 劉明洋著", href: "https://www.jb51.net/books/648114.html", imgSrc: "http://img.jbzj.com/do/uploads/litimg/181128/1H13HM103.jpg" }, { title: "JavaScript開發(fā)框架權(quán)威指南", href: "https://www.jb51.net/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ù),通過數(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); })();
感興趣的朋友可以使用在線HTML/CSS/JavaScript前端代碼調(diào)試運(yùn)行工具:http://tools.jb51.net/code/WebCodeRun 測(cè)試上述代碼運(yùn)行效果。
這樣,我們就可以做到,無限制定義自己的標(biāo)簽和item屬性了,而無需修改JS代碼。
更多關(guā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)容可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《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ì)有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。