-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmustache.js
60 lines (56 loc) · 1.73 KB
/
mustache.js
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
/** @version 0.4.2 */
function mustache(template, self, parent, invert) {
var render = mustache
var output = ""
var i
function get (ctx, path) {
path = path.pop ? path : path.split(".")
ctx = ctx[path.shift()]
ctx = ctx != null ? ctx : ""
return (0 in path) ? get(ctx, path) : ctx
}
self = Array.isArray(self) ? self : (self ? [self] : [])
self = invert ? (0 in self) ? [] : [1] : self
for (i = 0; i < self.length; i++) {
var childCode = ''
var depth = 0
var inverted
var ctx = (typeof self[i] == "object") ? self[i] : {}
ctx = Object.assign({}, parent, ctx)
ctx[""] = {"": self[i]}
template.replace(/([\s\S]*?)({{((\/)|(\^)|#)(.*?)}}|$)/g,
function(match, code, y, z, close, invert, name) {
if (!depth) {
output += code.replace(/{{{(.*?)}}}|{{(!?)(&?)(>?)(.*?)}}/g,
function(match, raw, comment, isRaw, partial, name) {
return raw ? get(ctx, raw)
: isRaw ? get(ctx, name)
: partial ? render(get(ctx, name), ctx)
: !comment ? new Option(get(ctx, name)).innerHTML
: ""
}
)
inverted = invert
} else {
childCode += depth && !close || depth > 1 ? match : code
}
if (close) {
if (!--depth) {
name = get(ctx, name)
if (/^f/.test(typeof name)) {
output += name.call(ctx, childCode, function (template) {
return render(template, ctx)
})
} else {
output += render(childCode, name, ctx, inverted)
}
childCode = ""
}
} else {
++depth
}
}
)
}
return output
}