-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathtemplate.html
44 lines (41 loc) · 1011 Bytes
/
template.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="format-detection" content="telephone=no"/>
<meta name="screen-orientation" content="portrait">
<title></title>
</head>
<body>
<div class="content"></div>
<template id="template">
<p>name: {{name}}</p>
<p>age: {{age}}</p>
</template>
<script type="text/javascript">
var match;
var tpl = document.querySelector('#template');
var content = document.querySelector(".content");
var clone = document.importNode(tpl.content, true);
content.appendChild(clone);
// 模板引擎函数(类似MVC中的controller)
var mtpl = function (tpl, data) {
var re = /{{(.+?)}}/g;
while ( (match = re.exec(tpl)) !== null ) {
console.log(match);
if (match[1]) {
tpl = tpl.replace(match[0], data[match[1]])
} else {
tpl = tpl.replace(match[0], '')
}
}
return tpl;
}
var b = mtpl(tpl.innerHTML, {
name: 'haha',
age: 19,
a: 'sdf',
})
</script>
</body>
</html>