-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
258 lines (215 loc) · 6.91 KB
/
test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>jtmpl前端编译模板</title>
</head>
<body>
<script>
(function(jtmpl, win){
win.jtmpl = jtmpl();
}(function(){
var jtmpl = {};
jtmpl.cache = {};
jtmpl.LEFT_DELIMITER = jtmpl.LEFT_DELIMITER || "<%";
jtmpl.RIGHT_DELIMITER = jtmpl.RIGHT_DELIMITER || "%>";
var Ler = jtmpl.LEFT_DELIMITER, Rer = jtmpl.RIGHT_DELIMITER;
if(!String.prototype.trim){
String.prototype.trim = function(){
return this.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '');
}
}
var _type = function(obj){
return /\[object\s+(\w+)/.exec(Object.prototype.toString.call(obj).toLowerCase())[1];
}
var isObject = function(o){
var rst = Object.prototype.toString.call(o);
return rst == '[object Function]' || rst == '[object Object]';
}
// 编译模板并传递数据
jtmpl.template = function(tmpl, data){
if (!tmpl){
return;
}
var str = jtmpl.load(tmpl), rst = jtmpl.compile((str||'').trim(), data);
return rst;
}
// html过滤
jtmpl.encodeHTML = function(str){
var htmlCode = {
"'" : "'",
"\"" : """,
"&" : "&",
">" : ">",
"<" : "<",
"\\" : "\",
"/" : "/"
}
return String(str == null ? "" : str).replace(/['"<>&\/\\]/g, function($0){
return htmlCode[$0]||"";
});
}
// 正则编码
jtmpl.encodeRegExp = function(str){
return String(str).replace(/([.*+?^=!:${}()|[\]/\\])/g,'\\$1');
}
// 清除换行
jtmpl.encodeBr = function(str){
return String(str == null ? "" : str).replace(/[\t\n\r\v]/g, "");
}
// 清除注释
jtmpl.clearAnnotation = function (str){
return String(str)
.replace(new RegExp(Ler + "??\\s*?//.*?\\s*?" + Rer,"g"), "")
.replace(new RegExp("<!--.*?-->", "g"),"")
.replace(new RegExp(Ler+"\\*.*?\\*"+Rer, "g"),"");
}
// 模板编译
jtmpl.compile = function(str, data){
var _rst_fn;
// 缓存掉编译结果,省略掉了模板再次加载编译过程
if (jtmpl.cache[str]){
_rst_fn = jtmpl.cache[str];
}else {
var fbody = "var _fbody_arr = [];"
+ "\nvar _fn = (function(_data_obj){"
+ "\nwith(_data_obj){\n_fbody_arr.push('"
+ _javascript(str)
+ "');\n}\n}(data));"
+ "\nfn=null;\nreturn _fbody_arr.join('');";
_rst_fn = new Function('data', fbody);
jtmpl.cache[str] = _rst_fn;
}
// 如果传入数据,返回编辑完的html,否则返回模板编译函数
return /\[object\s+(?:Function|Object)\]/g.test(Object.prototype.toString.call(data)) ? _rst_fn(data) : _rst_fn;
}
// 处理子模板
jtmpl.include = function(str){
return String(str == null ? "" : str)
.replace(new RegExp(Ler + '\\s*?p(?:art)?\\s*?\\:\\s*?([\\w-]+)\\s*?' + Rer,'g'), function(str, id){
return jtmpl.load('#' + id);
});
}
// 组装
jtmpl.group = function(str){
return String(str == null ? "" : str)
.replace(new RegExp(Ler + '\\s*?g(?:roup)?\\s*?\\:\\s*?([\\w-,\\s]+)\\s*?' + Rer,'g'), function(str, groups){
var groups = groups.split(','), shtml='';
console.log(groups)
for (var i=0, len=groups.length; i<len; i++){
shtml += jtmpl.load('#' + groups[i].trim())
}
console.log(shtml)
return shtml;
});
}
// 加载模板格式
jtmpl.load = function(str, callback){
var refer, elem, html, cache = jtmpl.cache;
if (refer = (/f(?:ile)?\:([A-Za-z0-9\u4E00-\u9FA5/?&.:=""]+)/.exec(str)||[])[1]){
// 外部模板需要加载,异步执行回调
callback(refer);
}else if (refer = (/#([\w-$]+)/.exec(str)||[])[1]){
elem = document.getElementById(refer);
html = jtmpl.include(elem && elem.tagName && /input|textarea/ig.test(elem.tagName)
? elem.value : elem.innerHTML);
return html;
}else {
return jtmpl.group(str);
}
}
/**
* 直接渲染到容器
* @ str 模板id
* @ el目标容器
* @ data数据
* @ 其实处理方式,例如jq可以用回调处理
**/
jtmpl.render = function(tmpl, el, data, callback){
if (!tmpl || typeof(tmpl) != 'string' || !el || !data || !isObject(data)){
return;
}
var elem = typeof(el) == 'string' ? document.getElementById(el) : el;
// 如果是dom元素直接innerHTML,否则交给回调适情况处理
if (elem && elem.nodeName){
elem.innerHTML = jtmpl.compile(tmpl, data);
}else if (!!callback){
callback(elem, jtmpl.compile(tmpl, data));
}
}
var _javascript = function(str){
// 清除注释和换行
str = jtmpl.encodeBr(jtmpl.clearAnnotation(str));
str = str.replace(new RegExp("("+Ler+"\\s*?[html]*?\:??\\s*?\=\\s*?[^;|"+Rer+"]+?\\s*?)" + Rer, 'g'), "$1\;" + Rer)
.replace(new RegExp(Ler + '\\s*?h(?:tml)??\:\\s*?\=\\s*?([^;|'+Rer+']+?);?\\s*?' + Rer, 'g'), "',typeof($1) === 'undefined' ? '' : jtmpl.encodeHTML($1),'")
.replace(new RegExp(Ler + '\\s*?\=\\s*?([^;|'+Rer+']+?);\\s*?' + Rer, 'g'), "',typeof($1) === 'undefined' ? '' : $1,'");
return str.split(Ler).join("');").split(Rer).join("_fbody_arr.push('");
};
jtmpl._javascript = _javascript;
return jtmpl;
}, window));
</script>
<script id="friends" type="text/jtmpl">
<dl>
<%for (var f=0,flen=friends.length; f<flen; f++){%>
<dt><%=friends[f].name%></dt>
<dd><%=friends[f].age%></dd>
<%}%>
</dl>
</script>
<script id="header" type="text/jtmpl">
<hgroup>
<p>"dddd"</p>
<% for (var j=0,jlen=headers.length; j<jlen; j++) {%>
<h2><% h:=headers[j]%></h2>
<%}%>
</hgrop>
</script>
<script id="footer" type="text/jtmpl">
<!--dafafa fdr -->
<%*dfafafafdr%&$*%>
<footer><%=info;%></footer>
</script>
<script id="test" type="text/jtmpl">
<%part:header%>
<%
// fafafrrr
%>
<% // fafafa%%>
<ul>
<%for (var i=0, len = data.length; i<len; i++){%>
<li><%=data[i]%></li>
<%}%>
</ul>
<%p:footer%>
</script>
<script id="a" type="text/jtmpl">
<p><%=name%></p>
</script>
<script id="b" type="text/jtmpl">
<p><%=age%></p>
</script>
<script id="c" type="text/jtmpl">
<p><%=work%></p>
</script>
<script>
var html = jtmpl.template('#test', {
headers : ["标题一", "标题二"],
data : ["内容一", "内容二"],
info : "模板测试"
});
/*console.log(tmpl_fn({
headers : ["<i>标题一</i>", "标题2"],
data : ["内容一", "内容二"],
info : "模板测试"
}))*/
console.log(html)
document.body.innerHTML += html;
var fhtml = jtmpl.template('#friends', {
friends : [{name:'tom', age : 28}, {name : 'lucy', age : 29}]
});
console.log(jtmpl.template('<%html:=name%>', {name : '<i>lucy</i>'}));
console.log(jtmpl.template('<%group:a, b, c%>', {name : 'mongo', age : 26, work : 'famer'}));
</script>
</body>
</html>