/
simple-template.html
62 lines (54 loc) · 1.5 KB
/
simple-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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>hello</title>
<meta name="description" content="">
<meta name="valwords" content="">
<!-- <link rel="shortcut icon" href="favicon.ico"> -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 默认使用webkit内核 -->
<meta name="renderer" content="webkit">
<!-- 兼容模式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link href="" rel="stylesheet">
</head>
<body>
<div id='a'></div>
<script>
function compile(template) {
const evalExpr = /<%=(.+?)%>/g;
const expr = /<%([\s\S]+?)%>/g;
template = template
.replace(evalExpr, '`); \n echo( $1 ); \n echo(`')
.replace(expr, '`); \n $1 \n echo(`');
template = 'echo(`' + template + '`);';
let script =
`(function parse(data){
let output = "";
function echo(html){
output += html;
}
${ template}
return output;
})`;
return eval(script);
}
let template = `
<ul>
<% for(let i=0; i < data.supplies.length; i++) { %>
<li><%= data.supplies[i] %></li>
<% } %>
</ul>
`;
let div = document.getElementById('a');
let parse = compile(template);
div.innerHTML = parse({ supplies: ["broom", "mop", "cleaner"] });
// <ul>
// <li>broom</li>
// <li>mop</li>
// <li>cleaner</li>
// </ul>
</script>
</body>
</html>