-
Notifications
You must be signed in to change notification settings - Fork 45
/
render.js
40 lines (32 loc) · 967 Bytes
/
render.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
/**
* Template renderer and cacher
*/
define(["lodash", "hogan", "core/status", "i18n/i18n", "core/templates"], function(_, Hogan, Status, Translate, templates) {
var cache = templates.compiled || {},
raw = templates.raw || {},
i18n = Translate.getAll();
var render = function(template, data, partials) {
var compiled = cache[template];
// Partials don't work with precompiled templates
if (!compiled || partials) {
if (typeof raw[template] !== "undefined") {
try {
compiled = cache[template] = Hogan.compile(raw[template]);
}
catch (e) {
Status.error("An error occurred while trying to render the " + template + " template!");
}
}
if (!compiled) {
return 'Template "' + template + '" not found!';
}
}
data = _.clone(data || {});
if (!data.i18n) data.i18n = i18n;
return compiled.render(data, partials);
};
render.getRaw = function(template) {
return raw[template];
};
return render;
});