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
/**
* Created by liu on 15-9-19.
*/
var jo={};
(function(_) {
var _uid=0;
_.uid= function () {
return ++_uid
}
_.escape = function(string) {
return (''+string).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g,'/');
};
var txnd = {};
_._textNode = function(pid, s) {
if (!(pid in txnd))
txnd[pid] = [];
txnd[pid].push(s);
s= '<abbr id="jo_txnd'+txnd[pid].length+'">joO txnd</abbr>';
return s;
};
_.template_end = function(pid) {
var ps = txnd[pid], abbr;
if (!ps || !ps.length) return;
for (var i=1,l=ps.length; i<=l; i++) {
abbr = _.G('jo_txnd'+i);
abbr.parentNode.replaceChild(
document.createTextNode(ps[i-1]), abbr );
}
abbr = ps = null;
delete txnd[pid];
};
_.templateSettings = {
evaluate : /{%([\s\S]+?)%}/g,
interpolate : /{%=([\s\S]+?)%}/g,
escape : /{%-([\s\S]+?)%}/g
};
_.template = function(str) {
var pid = this.uid();
var c = _.templateSettings;
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
'with(obj||{}){__p.push(\'' +
str.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(c.escape, function(match, code) {
return "',jo._textNode("+pid+"," + code.replace(/\\'/g, "'") + "),'";
})
.replace(c.interpolate, function(match, code) {
return "'," + code.replace(/\\'/g, "'") + ",'";
})
.replace(c.evaluate || null, function(match, code) {
return "');" + code.replace(/\\'/g, "'")
.replace(/[\r\n\t]/g, ' ') + "__p.push('";
})
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
+ "');}return __p.join('');";
var func = new Function('obj', tmpl);
return [func, pid];
};
_.txnd = txnd; // 反正也不打算长久用,先暴露出来吧,方便调试
})(jo);
jo.taTpl = function(taSel, data, posSel) {
if (!data) return;
var html = taSel.html();
html = jo.template(html)[0](data); // null 值会被 join 干掉
if (posSel) {
taSel.remove();
taSel = posSel;
}
taSel.replaceWith(html);
};
jo.reTpl = function(taSel, data, posSel) {
if (!data) return;
var html = taSel.html();
html = jo.template(html)[0](data);
posSel.empty().append(html);
};