-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2016-template-engine.html
171 lines (145 loc) · 4.37 KB
/
day2016-template-engine.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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Template Engine</title>
<script src="https://cdn.staticfile.net/jquery/3.7.1/jquery.js"></script>
<style>
body {
max-width: 400px;
background-color: #101010;
color: white;
font-size: 20px;
font-family: Consolas, NSimSun, serif;
}
main#render-target {
outline: solid 2px #ff7200;
}
.notes > .note {
list-style: simp-chinese-informal;
}
</style>
</head>
<body>
<script type="text/x-te-template" id="list-with-title">
<div class="te-list-wrap">
<h2>{{ ctx.title }}</h2>
<ul>
<li>{{ {a:10,b:20} }}</li>
<li>Title's length is {{ {{}} ctx.title.length }}</li>
<li>{{ ctx.title.length > 10 ? 'maybe too long' : 'may be too short' }}</li>
<li>{{@ <div> verbatim-1 </div>}}</li>
<li>{{@ }}{{}} curly braces }}</li>
<li>{{@ verbatim-2 {{ []}} {{ }} }}</li>
<li>}}{{}}</li>
</ul>
{{if 20 > 10}}
<p>This text should be rendered, 20>10 is {{20>10}}</p>
{{/if}}
{{if !(20 > 10)}}
<p>This text should be not rendered, !(20 > 10) is {{!(20 > 10)}}</p>
{{/if}}
{{if 20 > 10}}
{{if 20 > 15}}
<p>20 > 15</p>
{{/if}}
{{if 20 < 15}}
<p>20 < 15</p>
{{/if}}
<p>20>10</p>
{{/if}}
</div>
</script>
<main id="render-target"></main>
<ul class="notes">
<li class="note">统统使用贪心正则,如果表达式求值出错,则视为 {{@ }}</li>
</ul>
<script>
function isObject(value) {
const type = typeof value;
return !!value && (type === "object" || type === "function");
}
</script>
<script>
function renderToText(tpl, ctx) {
const doubleCurlyBraces = /{{(.*)}}/g;
ctx.__conditionalBlockStack = ctx.__conditionalBlockStack ?? [];
return tpl.replaceAll(doubleCurlyBraces, dispatch);
function dispatch(all, inner, index, template) {
function betterEval(expr, ...args) {
return (new Function("ctx", `return (${expr});`))(...args);
}
function emitJsExpr(_, expr) {
let value;
try {
value = betterEval(expr, ctx);
} catch {
return emitVerbatim(_, expr, true);
}
if (isObject(value)) {
const tag = Object.prototype.toString.call(value);
if (tag === "[object Function]") {
return value.toString();
}
return JSON.stringify(value);
}
return value;
}
function emitVerbatim(a, i, mustEmit = false) {
if (mustEmit === true || a.startsWith("{{@")) {
return i.slice(1);
}
return null;
}
function ignoreEmpty(_, i) {
if (i.trim().length === 0) {
return "";
}
return null;
}
function emitConditionalBlock(a, i) {
const isCondStart = a.startsWith("{{if");
const isCondEnd = a.startsWith("{{/if");
if (!isCondStart && !isCondEnd) {
return null;
}
if (isCondStart) {
const booleanExpr = i.slice(2);
const shouldExec = betterEval(booleanExpr);
ctx.__conditionalBlockStack.push(shouldExec);
console.log(`conditional block starts ${booleanExpr} = ${shouldExec}`);
return "";
}
if (isCondEnd) {
ctx.__conditionalBlockStack.pop();
console.log("conditional block ends");
return "";
}
}
const emitters = [
ignoreEmpty,
emitConditionalBlock,
emitVerbatim,
emitJsExpr,
];
for (const echo of emitters) {
const result = echo(all, inner, index, template);
if (result !== null) {
const fnName = echo.toString().match(/function (\w+)/)[1];
console.log(fnName, "echos", `^${result}$`);
return result;
}
}
return "!!error!!";
}
}
const context = {
title: "Title rendered by te",
};
const templateText = $("#list-with-title").text().trim();
const rendered = renderToText(templateText, context);
console.log("rendered\n", rendered);
$("main#render-target").html(rendered);
</script>
</body>
</html>