关于模板,写页面的人们其实一直在用,asp.net , jsp , php, nodejs等等都有他的存在,固然那是服务端的模板。 前端模板,做为前端人员确定是多少有接触的,Handlebars.js,JsRender,Dust.js,Mustache.js,Underscore templates,Angularjs,Vuejs,reactjs处处都离不开模板的影子。
关于前端模板的分类,我会在单独的博客来和你们一块儿学习。
本文主要是分析一下jQuery的创始人的Micro-Templating,麻雀虽小缺五张俱全。
先贴出做者的源码:javascript
// Simple JavaScript Templating // John Resig - https://johnresig.com/ - MIT Licensed (function(){ var cache = {}; this.tmpl = function tmpl(str, data){ // Figure out if we're getting a template, or if we need to // load the template - and be sure to cache the result. var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) : // Generate a reusable function that will serve as a template // generator (and which will be cached). new Function("obj", "var p=[],print=function(){p.push.apply(p,arguments);};" + // Introduce the data as local variables using with(){} "with(obj){p.push('" + // Convert the template into pure JavaScript str .replace(/[\r\t\n]/g, " ") .split("<%").join("\t") .replace(/((^|%>)[^\t]*)'/g, "$1\r") .replace(/\t=(.*?)%>/g, "',$1,'") .split("\t").join("');") .split("%>").join("p.push('") .split("\r").join("\\'") + "');}return p.join('');"); // Provide some basic currying to the user return data ? fn( data ) : fn; }; })();
关于 1,3,5没有太多须要讲的,关于5,若是执行时不传入data参数,返回的执行函数,能够延迟使用,处处使用。php
重点在于2和4,在这以前,先看看print,这个print申请在函数顶部,就表示在js语句的时候是能够调用呢,怎么调用呢,看看示例,至于做用么,固然是debug啊html
<script type="text/html" id="item_tmpl"> <% for ( var i = 0; i < items.length; i++ ) { %> <% if( i%2 == 1) {%> <li><%=items[i].id%>:<%=items[i].name%></li> <% } %> <% } %> <% print('数组长度' + items.length ); %> <div style='background:<%=color%>'><%=id%></div> </script>
很简单: <% print('数组长度' + items.length ); %>
原理也很简单,数组p里面添加一条数据前端
为了方便debug和备注,我调整一下原理结构java
(function () { var cache = {}; this.tmpl = function tmpl(str, data) { // Figure out if we're getting a template, or if we need to // load the template - and be sure to cache the result. var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) : // Generate a reusable function that will serve as a template // generator (and which will be cached). new Function("obj", "var p=[],print=function(){p.push.apply(p,arguments);};" + // Introduce the data as local variables using with(){} "with(obj){p.push('" + // Convert the template into pure JavaScript getStr(str) + "');}return p.join('');"); // Provide some basic currying to the user return data ? fn(data) : fn; }; function getStr(str){ // 删除回车,制表,换行 str = str .replace(/[\r\t\n]/g, " "); // 替换 <% 为 \t制表符,两种状况(赋值和js代码) // 赋值: 例如 <div id="<%=id%>"> ==> <div id="\t=id%>"> // js代码:例如 <% for ( var i = 0; i < items.length; i++ ) { %> ==> \t for ( var i = 0; i < items.length; i++ ) { %> str = str.split("<%").join("\t"); // 替换'为\r ,最后一步会从新替换回来 // 节点属性操做赋值使用单引号,若是不替换 ,''>' 是会报错的 // <div style='background:<%=color%>'><%=id%></div> ==> p.push(' <div style='background:',color,''>',id,'</div> '); str = str.replace(/((^|%>)[^\t]*)'/g, "$1\r"); // 赋值解析:赋值后部分,拆分为三项,结合with,id就会成为实际的值,而后一直被push <div id="\t=id%>"> ==> <div id=" ,id, "> // 这里会消费掉 <%=xxx%>, // 那么剩下的 %>必然是js语句结尾的, \t必然是js语句的开头 str = str.replace(/\t=(.*?)%>/g, "',$1,'"); //js语句开始符号替换: 通过上一步后,还剩余的\t,是js语句的,这里就用 ');来结束 ,js语句会单开p.push, str = str.split("\t").join("');"); // js语句结尾符号替换: %> 替换为 p.push, 这里把js语句内生成的字符串或者变量再push一次 str = str.split("%>").join("p.push('"); // 替换回车为\' , 恢复str.replace(/((^|%>)[^\t]*)'/g, "$1\r") 去掉的' str = str.split("\r").join("\\'"); return str; } })();
上面颇有意思的是,先彻底替换了\r\t,而后再用\r\t做为占位符。
\t做为<%的占位符,\r做为特定条件下'的占位符。node
咱们接下来按照正则替换一步异步来分析react
<% for ( var i = 0; i < items.length; i++ ) { %> <% if( i%2 == 0) {%> <li><%=items[i].id%>:<%=items[i].name%></li> <% } %> <% } %> <% print('数组长度' + items.length ); %> <div style='background:<%=color%>'><%=id%></div>
\n <% for ( var i = 0; i < items.length; i++ ) { %> \n <% if( i%2 == 0) {%>\n <li><%=items[i].id%>:<%=items[i].name%></li>\n <% } %> \n <% } %>\n <% print('数组长度' + items.length ); %>\n <div style='background:<%=color%>'><%=id%></div>\n
去掉回车,换行,制表编程
<% for ( var i = 0; i < items.length; i++ ) { %> <% if( i%2 == 0) {%> <li><%=items[i].id%>:<%=items[i].name%></li> <% } %> <% } %> <% print('数组长度' + items.length ); %> <div style='background:<%=color%>'><%=id%></div>
<%替换为\tsegmentfault
\t for ( var i = 0; i < items.length; i++ ) { %> \t if( i%2 == 0) {%> <li>\t=items[i].id%>:\t=items[i].name%></li> \t } %> \t } %> \t print('数组长度' + items.length ); %> <div style='background:\t=color%>'>\t=id%></div>
\t for ( var i = 0; i < items.length; i++ ) { %> \t if( i%2 == 0) {%> <li>\t=items[i].id%>:\t=items[i].name%></li> \t } %> \t } %> \t print('数组长度' + items.length ); %> <div style=\rbackground:\t=color%>\r>\t=id%></div>
\t for ( var i = 0; i < items.length; i++ ) { %> \t if( i%2 == 0) {%> <li>',items[i].id,':',items[i].name,'</li> \t } %> \t } %> \t print('数组长度' + items.length ); %> <div style=\rbackground:',color,'\r>',id,'</div>
剩下的\t,表明了js语句开始部分, js语句\t替换为'); ,正是push的结束部分,正好完成push语句数组
'); for ( var i = 0; i < items.length; i++ ) { %> '); if( i%2 == 0) {%> <li>',items[i].id,':',items[i].name,'</li> ');} %> '); } %> '); print('数组长度' + items.length ); %> <div style=\rbackground:',color,'\r>',id,'</div>
剩下的%>体表了js语句的结束,替换为p.push('",开启新的环节
'); for ( var i = 0; i < items.length; i++ ) { p.push(' '); if( i%2 == 0) {p.push(' <li>',items[i].id,':',items[i].name,'</li> '); } p.push(' '); } p.push(' '); print('数组长度' + items.length ); p.push(' <div style=\rbackground:',color,'\r>',id,'</div>
替换\r为' , 恢复str.replace(/((^|%>)[^\t]*)'/g, "$1\r") 去掉的'
'); for ( var i = 0; i < items.length; i++ ) { p.push(' '); if( i%2 == 0) {p.push(' <li>',items[i].id,':',items[i].name,'</li> '); } p.push(' '); } p.push(' '); print('数组长度' + items.length ); p.push(' <div style=\'background:',color,'\'>',id,'</div>
var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push(' '); for ( var i = 0; i < items.length; i++ ) { p.push(' '); if( i%2 == 0) {p.push(' <li>',items[i].id,':',items[i].name,'</li> '); } p.push(' '); } p.push(' '); print('数组长度' + items.length ); p.push(' <div style=\'background:',color,'\'>',id,'</div> ');}return p.join('');
最后格式化一下
var p = [], print = function () { p.push.apply(p, arguments); }; with (obj) { p.push(' '); for (var i = 0; i < items.length; i++) { p.push(' '); if (i % 2 == 0) { p.push(' < li > ', items[i].id, ': ', items[i].name, '</li > '); } p.push(' '); } p.push(' '); print('数组长度' + items.length); p.push(' < div style =\'background:', color, '\'>', id, '</div> '); } return p.join('');
源码中你会发现,时而replace,时而split + join,你们都很清楚的能够看出
split + join达到的效果是和replace彻底一致的。说到这里,你们确定都很明白了,效率
我简单作了个实验,源码以下,自行替换str的值,而后贴到控制台执行,我测试的内容是打开百度,
查看源码,把全部源码赋值过来,而后执行。
var str = ` blabla...................................... ` + Math.random(); console.log('str length:' + str.length) console.log('a count:' + str.match(/a/g).length) console.time('split-join-a') str.split('a').join('_a_') console.timeEnd('split-join-a') console.time('replace-a') str.replace(/a/g,'_a_') console.timeEnd('replace-a') console.log('window count:' + str.match(/window/g).length) console.time('split-join-window') str.split('window').join('_window_') console.timeEnd('split-join-window') console.time('replace-window') str.replace(/window/g,'_window_') console.timeEnd('replace-window')
执行结果
str length:114401 a count:4364 split-join-a: 4.521240234375ms replace-a: 13.24609375ms window count:29 split-join-window: 0.330078125ms replace-window: 0.297119140625ms
11万个字符,
当匹配项是4000多得时候,执行时间相差比较大 ,
当匹配项是29的时候,知晓效率相差并不大,不少时候,replace比split+join还快
注意注意,这里都是不带正则查找,建议就是匹配项多得时候,用split +join喽
这个模板如此简单,能不能担任重任。这是基于字符串模板,还有基于dom的模板,还有混合型的。
字符串模板的缺点抛开安全和性能,就是渲染后和页面分离了,要想再操做,就须要本身再去定制了。
假如是仅仅是列表展示,是至关好的。
一个对前端模板技术的全面总结
JavaScript 进阶之深刻理解数据双向绑定
模板引擎性能对比
最简单的JavaScript模板引擎
有哪些好用的前端模板引擎?
JavaScript Micro-Templating