-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
templates.js
145 lines (120 loc) · 2.86 KB
/
templates.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
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
// Total.js Templates
// The MIT License
// Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
exports.render = function(body, model, $) {
return new Promise(function(resolve, reject) {
var cache = F.temporary.templates;
var id = 'Ttemplate' + HASH(body);
var data = cache[id];
if (data) {
try {
resolve(data.template({ value: model || data.model }, null, data.helpers));
} catch (e) {
if ($ && $.invalid)
$.invalid(e);
else
reject(e);
}
return;
}
if (body.indexOf('{{') === -1) {
// URL address or filename
data = cache[id];
var start = body.substring(0, 7);
// http://
// https:/
if (start === 'http://' || start === 'https:/') {
// download template
var opt = {};
opt.url = body;
opt.method = 'GET';
opt.$ = function(err, response) {
if (err) {
if ($ && $.invalid)
$.invalid(err);
else
reject(err);
return;
}
data = parse(response.body);
cache[id] = data;
try {
resolve(data.template({ value: model || data.model }, null, data.helpers));
} catch (e) {
if ($ && $.invalid)
$.invalid(e);
else
reject(e);
}
};
REQUEST(opt);
return;
} else {
if (body[0] === '~') {
body = body.substring(1);
} else {
body = F.path.templates(body);
if (body.indexOf('.html') === -1)
body += '.html';
}
F.Fs.readFile(body, function(err, response) {
if (err) {
if ($ && $.invalid)
$.invalid(err);
else
reject(err);
return;
}
data = parse(response.toString('utf8'));
if (!DEBUG)
cache[id] = data;
try {
resolve(data.template({ value: model || data.model }, null, data.helpers));
} catch (e) {
if ($ && $.invalid)
$.invalid(e);
else
reject(e);
}
});
return;
}
}
data = cache[id] = parse(body);
try {
resolve(data.template({ value: model || data.model }, null, data.helpers));
} catch (e) {
if ($ && $.invalid)
$.invalid(e);
else
reject(e);
}
});
};
function parse(body) {
var helpers = {};
var model = EMPTYOBJECT;
var strhelpers = '';
var beg = body.indexOf('<scr' + 'ipt>');
var end;
// helpers
if (beg !== -1) {
end = body.indexOf('</scr' + 'ipt>', beg + 8);
strhelpers = body.substring(beg + 8, end).trim();
body = body.substring(0, beg) + body.substring(end + 9);
}
// model
beg = body.indexOf('<scr' + 'ipt type="text/json">');
if (beg !== -1) {
end = body.indexOf('</scr' + 'ipt>', beg + 8);
model = body.substring(beg + 25, end).trim().parseJSON(true);
body = body.substring(0, beg) + body.substring(end + 9);
}
if (strhelpers)
new Function('Thelpers', strhelpers)(helpers);
var output = {};
output.helpers = helpers;
output.template = Tangular.compile(body);
output.model = model;
return output;
}