国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

template.js前端模板引擎使用詳解

2019-11-19 15:12:18
字體:
來源:轉載
供稿:網(wǎng)友

本文介紹了template.js前端模板引擎使用,分享給大家,具體如下:

下載地址:https://github.com/yanhaijing/template.js

作者編寫的文檔:https://github.com/yanhaijing/template.js/blob/master/README.md

源碼學習

默認的開始標簽和結束標簽分別是:

  1. sTag: '<%',//開始標簽,可以重寫,我項目中使用的是<:
  2. eTag: '%>',//結束標簽,我項目中使用的是:>

快速上手

編寫模板

使用一個type=”text/html”的script標簽存放模板,或者放到字符串中:

<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>

轉義

<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轉義</td><td><%:h=html%></td></tr>      <tr><td><%:=html>不轉義</td><td><%:=html%></td></tr>      <tr><td><%:u=url>URI轉義</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>默認輸出不進行轉義<%=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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凉山| 自贡市| 漾濞| 新化县| 墨玉县| 建阳市| 肃宁县| 西青区| 青海省| 涞水县| 普安县| 静海县| 民乐县| 海安县| 建昌县| 桦南县| 兰考县| 台安县| 木里| 高州市| 仁化县| 翁牛特旗| 祁连县| 杨浦区| 含山县| 乐山市| 宽城| 忻州市| 含山县| 永安市| 淮滨县| 鄢陵县| 永寿县| 玉龙| 岢岚县| 平山县| 福泉市| 宝坻区| 灵山县| 灵丘县| 新龙县|