下载地址
https://github.com/yanhaijing/template.js
作者编写的文档:https://github.com/yanhaijing/template.js/blob/master/README.md
源码学习
默认的开始标签和结束标签分别是:
sTag: '<%',//开始标签,可以重写,我项目中使用的是<:
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循环输出两次</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>
注册函数
<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>