forked from dunizb/CodeTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIV模板引擎.html
53 lines (48 loc) · 1.81 KB
/
DIV模板引擎.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>DIV模板引擎</title>
</head>
<body>
<h1>DIV模板引擎</h1>
<script>
var Template = function (tempstr, data) {
var re = /<%([^%>]+)?%>/g,
cursor = 0, // 字符串的查找位置
reExp = /^\s*(for|if|else|switch|case|break|continue|{|})(.*)?/g,
code = 'var arr = [];\n';
var add = function (line, flag) { //flag --> 判断是否是JS代码
// 第一次:line ==> 我的名字是:
code += flag ? line.match(reExp) ? line : 'arr.push(' + line + ');\n'
: 'arr.push("' + line.replace(/"/, '\\"') + '");\n';
}
while (item = re.exec(tempstr)) {
add(tempstr.slice(cursor, item.index));
add(item[1], true);
cursor = item.index + item[0].length;
}
add(tempstr.substring(cursor));
code += 'return arr.join("");';
return new Function(code.replace(/[\r\n\t]/g, '')).apply(data);
}
// var str = '我的名字是:<%this.name%>,年龄是:<%this.profile.age%>,Over!!!';
// var res = Template(str, {
// name: 'Dunizb',
// profile: {
// age: 22
// }
// });
// console.log(res);
var str = '我的爱好:<%for(var i in this.hobby){%>'
+ '<p><%this.hobby[i]%></p>'
+ '<%}%>';
var res = Template(str, {
hobby: ['php', 'java', 'javascript', 'linux', 'python']
});
document.write(res);
</script>
</body>
</html>