Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
define(
2
["./utils","./exception","./base","exports"],
3
function(__dependency1__, __dependency2__, __dependency3__, __exports__) {
4
"use strict";
5
var Utils = __dependency1__;
6
var Exception = __dependency2__["default"];
7
var COMPILER_REVISION = __dependency3__.COMPILER_REVISION;
8
var REVISION_CHANGES = __dependency3__.REVISION_CHANGES;
9
var createFrame = __dependency3__.createFrame;
10
11
function checkRevision(compilerInfo) {
12
var compilerRevision = compilerInfo && compilerInfo[0] || 1,
13
currentRevision = COMPILER_REVISION;
14
15
if (compilerRevision !== currentRevision) {
16
if (compilerRevision < currentRevision) {
17
var runtimeVersions = REVISION_CHANGES[currentRevision],
18
compilerVersions = REVISION_CHANGES[compilerRevision];
19
throw new Exception("Template was precompiled with an older version of Handlebars than the current runtime. "+
20
"Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").");
21
} else {
22
// Use the embedded version info since the runtime doesn't know about this revision yet
23
throw new Exception("Template was precompiled with a newer version of Handlebars than the current runtime. "+
24
"Please update your runtime to a newer version ("+compilerInfo[1]+").");
25
}
26
}
27
}
28
29
__exports__.checkRevision = checkRevision;// TODO: Remove this line and break up compilePartial
30
31
function template(templateSpec, env) {
32
/* istanbul ignore next */
33
if (!env) {
34
throw new Exception("No environment passed to template");
35
}
36
if (!templateSpec || !templateSpec.main) {
37
throw new Exception('Unknown template object: ' + typeof templateSpec);
38
}
39
40
// Note: Using env.VM references rather than local var references throughout this section to allow
41
// for external users to override these as psuedo-supported APIs.
42
env.VM.checkRevision(templateSpec.compiler);
43
44
var invokePartialWrapper = function(partial, context, options) {
45
if (options.hash) {
46
context = Utils.extend({}, context, options.hash);
47
}
48
49
partial = env.VM.resolvePartial.call(this, partial, context, options);
50
var result = env.VM.invokePartial.call(this, partial, context, options);
51
52
if (result == null && env.compile) {
53
options.partials[options.name] = env.compile(partial, templateSpec.compilerOptions, env);
54
result = options.partials[options.name](context, options);
55
}
56
if (result != null) {
57
if (options.indent) {
58
var lines = result.split('\n');
59
for (var i = 0, l = lines.length; i < l; i++) {
60
if (!lines[i] && i + 1 === l) {
61
break;
62
}
63
64
lines[i] = options.indent + lines[i];
65
}
66
result = lines.join('\n');
67
}
68
return result;
69
} else {
70
throw new Exception("The partial " + options.name + " could not be compiled when running in runtime-only mode");
71
}
72
};
73
74
// Just add water
75
var container = {
76
strict: function(obj, name) {
77
if (!(name in obj)) {
78
throw new Exception('"' + name + '" not defined in ' + obj);
79
}
80
return obj[name];
81
},
82
lookup: function(depths, name) {
83
var len = depths.length;
84
for (var i = 0; i < len; i++) {
85
if (depths[i] && depths[i][name] != null) {
86
return depths[i][name];
87
}
88
}
89
},
90
lambda: function(current, context) {
91
return typeof current === 'function' ? current.call(context) : current;
92
},
93
94
escapeExpression: Utils.escapeExpression,
95
invokePartial: invokePartialWrapper,
96
97
fn: function(i) {
98
return templateSpec[i];
99
},
100
101
programs: [],
102
program: function(i, data, declaredBlockParams, blockParams, depths) {
103
var programWrapper = this.programs[i],
104
fn = this.fn(i);
105
if (data || depths || blockParams || declaredBlockParams) {
106
programWrapper = program(this, i, fn, data, declaredBlockParams, blockParams, depths);
107
} else if (!programWrapper) {
108
programWrapper = this.programs[i] = program(this, i, fn);
109
}
110
return programWrapper;
111
},
112
113
data: function(data, depth) {
114
while (data && depth--) {
115
data = data._parent;
116
}
117
return data;
118
},
119
merge: function(param, common) {
120
var ret = param || common;
121
122
if (param && common && (param !== common)) {
123
ret = Utils.extend({}, common, param);
124
}
125
126
return ret;
127
},
128
129
noop: env.VM.noop,
130
compilerInfo: templateSpec.compiler
131
};
132
133
var ret = function(context, options) {
134
options = options || {};
135
var data = options.data;
136
137
ret._setup(options);
138
if (!options.partial && templateSpec.useData) {
139
data = initData(context, data);
140
}
141
var depths,
142
blockParams = templateSpec.useBlockParams ? [] : undefined;
143
if (templateSpec.useDepths) {
144
depths = options.depths ? [context].concat(options.depths) : [context];
145
}
146
147
return templateSpec.main.call(container, context, container.helpers, container.partials, data, blockParams, depths);
148
};
149
ret.isTop = true;
150
151
ret._setup = function(options) {
152
if (!options.partial) {
153
container.helpers = container.merge(options.helpers, env.helpers);
154
155
if (templateSpec.usePartial) {
156
container.partials = container.merge(options.partials, env.partials);
157
}
158
} else {
159
container.helpers = options.helpers;
160
container.partials = options.partials;
161
}
162
};
163
164
ret._child = function(i, data, blockParams, depths) {
165
if (templateSpec.useBlockParams && !blockParams) {
166
throw new Exception('must pass block params');
167
}
168
if (templateSpec.useDepths && !depths) {
169
throw new Exception('must pass parent depths');
170
}
171
172
return program(container, i, templateSpec[i], data, 0, blockParams, depths);
173
};
174
return ret;
175
}
176
177
__exports__.template = template;function program(container, i, fn, data, declaredBlockParams, blockParams, depths) {
178
var prog = function(context, options) {
179
options = options || {};
180
181
return fn.call(container,
182
context,
183
container.helpers, container.partials,
184
options.data || data,
185
blockParams && [options.blockParams].concat(blockParams),
186
depths && [context].concat(depths));
187
};
188
prog.program = i;
189
prog.depth = depths ? depths.length : 0;
190
prog.blockParams = declaredBlockParams || 0;
191
return prog;
192
}
193
194
__exports__.program = program;function resolvePartial(partial, context, options) {
195
if (!partial) {
196
partial = options.partials[options.name];
197
} else if (!partial.call && !options.name) {
198
// This is a dynamic partial that returned a string
199
options.name = partial;
200
partial = options.partials[partial];
201
}
202
return partial;
203
}
204
205
__exports__.resolvePartial = resolvePartial;function invokePartial(partial, context, options) {
206
options.partial = true;
207
208
if(partial === undefined) {
209
throw new Exception("The partial " + options.name + " could not be found");
210
} else if(partial instanceof Function) {
211
return partial(context, options);
212
}
213
}
214
215
__exports__.invokePartial = invokePartial;function noop() { return ""; }
216
217
__exports__.noop = noop;function initData(context, data) {
218
if (!data || !('root' in data)) {
219
data = data ? createFrame(data) : {};
220
data.root = context;
221
}
222
return data;
223
}
224
});
225