template.js前端模板引擎使用詳解
本文介紹了template.js前端模板引擎使用,分享給大家,具體如下:
下載地址:https://github.com/yanhaijing/template.js
作者編寫的文檔:https://github.com/yanhaijing/template.js/blob/master/README.md
源碼學(xué)習(xí)
默認的開始標(biāo)簽和結(jié)束標(biāo)簽分別是:
- sTag: '<%',//開始標(biāo)簽,可以重寫,我項目中使用的是<:
- eTag: '%>',//結(jié)束標(biāo)簽,我項目中使用的是:>
快速上手
編寫模板
使用一個type=”text/html”的script標(biāo)簽存放模板,或者放到字符串中:
<script id="tpl" type="text/html"> <ul> <%for(var i = 0; i < list.length; i++) {%> <li><%:=list[i].name%></li> <%}%> </ul> </script>
渲染模板
var tpl = document.getElementById('tpl').innerHTML; template(tpl, {list: [{name: "yan"},{name: "haijing"}]});
輸出:
<ul> <li>yan</li> <li>haijing</li> </ul>
轉(zhuǎn)義
<script id="tpl" type="text/html"> <table> <caption>for循環(huán)輸出兩次</caption> <%var test = '輸出自定義變量';%> <%for (var i = 0; i < 2; i++) {%> <tr><td><%=html%>默認</td><td><%=html%></td></tr> <tr><td><%:h=html>html轉(zhuǎn)義</td><td><%:h=html%></td></tr> <tr><td><%:=html>不轉(zhuǎn)義</td><td><%:=html%></td></tr> <tr><td><%:u=url>URI轉(zhuǎn)義</td><td><%:u=url%></td></tr> <tr><td>var</td><td><%:=test%></td></tr> <tr><td><%=test + 1>表達式</td><td><%=test + 1%></td></tr> <%if (true) {%> <tr><td>if</td><td>if 語句</td></tr> <%}%> <tr><td>分割線</td><td>------------------------------</td></tr> <%}%> </table> </script> <script src="../template.js"></script> <script> var html = template(document.getElementById('tpl').innerHTML, { url: 'http://yanhaijing.com?name=顏海鏡', html: '<div id="test">template.js "</div>' }); console.log(html); document.getElementById('wp').innerHTML = html; </script>
<script> template.config({sTag: '<#', eTag: '#>'}); var tpl1 = '<div><#:=name#></div>'; console.log('<##>:', template(tpl1, {name: '更改tag<##>'})); template.config({sTag: '{{', eTag: '}}'}); var tpl1 = '<div>{{:=name}}</div>'; console.log('{{}}:', template(tpl1, {name: '更改tag{{}}'})); template.config({sTag: '<%', eTag: '#>'}); var tpl1 = '<div><%:=name#></div>'; console.log('<%#>:', template(tpl1, {name: '不一致也可以哦,更改tag<%#>'})); template.config({sTag: '<%', eTag: '%>', compress: true}); var tpl1 = '<div>空格會被壓縮 空格 空格</div>'; console.log('compress:', template(tpl1, {})); template.config({sTag: '<%', eTag: '%>', escape: false}); var tpl1 = '<div>默認輸出不進行轉(zhuǎn)義<%=html%></div>'; console.log('escape:', template(tpl1, {html: '<div>html</div>'})); </script>
注冊函數(shù)
<div id="wp"></div> <script id="tpl" type="text/html"> <%=dateFormat(Date.now(), 'yyyy年 MM月 dd日 hh:mm:ss')%> </script> <script src="../template.js"></script> <script> template.registerFunction('dateFormat', function (date, format) { date = new Date(date); var map = { "M": date.getMonth() + 1, //月份 "d": date.getDate(), //日 "h": date.getHours(), //小時 "m": date.getMinutes(), //分 "s": date.getSeconds(), //秒 "q": Math.floor((date.getMonth() + 3) / 3), //季度 "S": date.getMilliseconds() //毫秒 }; format = format.replace(/([yMdhmsqS])+/g, function(all, t){ var v = map[t]; if(v !== undefined){ if(all.length > 1){ v = '0' + v; v = v.substr(v.length-2); } return v; } else if(t === 'y'){ return (date.getFullYear() + '').substr(4 - all.length); } return all; }); return format; }); console.log(template.registerFunction()); console.log(template.registerFunction('dateFormat')); </script> <script> var html = template(document.getElementById('tpl').innerHTML, {}); console.log(html); document.getElementById('wp').innerHTML = html; template.unregisterFunction('dateFormat'); console.log(template.registerFunction('dateFormat')); </script> <div id="wp"></div> <script id="tpl" type="text/html"> <%:up='yanhaijing'%> </script> <script src="../template.js"></script> <script> template.registerModifier('up', function (str) { return str.toUpperCase(); }); console.log(template.registerModifier()); console.log(template.registerModifier('up')); </script> <script> var html = template(document.getElementById('tpl').innerHTML, {}); console.log(html); document.getElementById('wp').innerHTML = html; template.unregisterModifier('up'); console.log(template.registerModifier('up')); </script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實現(xiàn)不使用圖片仿Windows右鍵菜單效果代碼
這篇文章主要介紹了JS實現(xiàn)不使用圖片仿Windows右鍵菜單效果代碼,涉及文鼎字及css樣式的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10JS實現(xiàn)根據(jù)詳細地址獲取經(jīng)緯度功能示例
這篇文章主要介紹了JS實現(xiàn)根據(jù)詳細地址獲取經(jīng)緯度功能,涉及javascript與百度地圖接口交互進行地址經(jīng)緯度查詢的相關(guān)操作技巧,需要的朋友可以參考下2019-04-04JavaScript實現(xiàn)數(shù)組去重的7種方法
去重是開發(fā)中經(jīng)常會碰到的一個熱點問題,本文詳細的介紹了JavaScript實現(xiàn)數(shù)組去重的7種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03javascript實現(xiàn)數(shù)組內(nèi)值索引隨機化及創(chuàng)建隨機數(shù)組的方法
這篇文章主要介紹了javascript實現(xiàn)數(shù)組內(nèi)值索引隨機化及創(chuàng)建隨機數(shù)組的方法,涉及javascript數(shù)組索引及隨機數(shù)的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08