1 import * as Utils from './utils';
2 import Exception from './exception';
3 import { COMPILER_REVISION, REVISION_CHANGES, createFrame } from './base';
5 export function checkRevision(compilerInfo) {
6 const compilerRevision = compilerInfo && compilerInfo[0] || 1,
7 currentRevision = COMPILER_REVISION;
9 if (compilerRevision !== currentRevision) {
10 if (compilerRevision < currentRevision) {
11 const runtimeVersions = REVISION_CHANGES[currentRevision],
12 compilerVersions = REVISION_CHANGES[compilerRevision];
13 throw new Exception('Template was precompiled with an older version of Handlebars than the current runtime. ' +
14 'Please update your precompiler to a newer version (' + runtimeVersions + ') or downgrade your runtime to an older version (' + compilerVersions + ').');
16 // Use the embedded version info since the runtime doesn't know about this revision yet
17 throw new Exception('Template was precompiled with a newer version of Handlebars than the current runtime. ' +
18 'Please update your runtime to a newer version (' + compilerInfo[1] + ').');
23 export function template(templateSpec, env) {
24 /* istanbul ignore next */
26 throw new Exception('No environment passed to template');
28 if (!templateSpec || !templateSpec.main) {
29 throw new Exception('Unknown template object: ' + typeof templateSpec);
32 templateSpec.main.decorator = templateSpec.main_d;
34 // Note: Using env.VM references rather than local var references throughout this section to allow
35 // for external users to override these as psuedo-supported APIs.
36 env.VM.checkRevision(templateSpec.compiler);
38 function invokePartialWrapper(partial, context, options) {
40 context = Utils.extend({}, context, options.hash);
42 options.ids[0] = true;
46 partial = env.VM.resolvePartial.call(this, partial, context, options);
47 let result = env.VM.invokePartial.call(this, partial, context, options);
49 if (result == null && env.compile) {
50 options.partials[options.name] = env.compile(partial, templateSpec.compilerOptions, env);
51 result = options.partials[options.name](context, options);
55 let lines = result.split('\n');
56 for (let i = 0, l = lines.length; i < l; i++) {
57 if (!lines[i] && i + 1 === l) {
61 lines[i] = options.indent + lines[i];
63 result = lines.join('\n');
67 throw new Exception('The partial ' + options.name + ' could not be compiled when running in runtime-only mode');
73 strict: function(obj, name) {
75 throw new Exception('"' + name + '" not defined in ' + obj);
79 lookup: function(depths, name) {
80 const len = depths.length;
81 for (let i = 0; i < len; i++) {
82 if (depths[i] && depths[i][name] != null) {
83 return depths[i][name];
87 lambda: function(current, context) {
88 return typeof current === 'function' ? current.call(context) : current;
91 escapeExpression: Utils.escapeExpression,
92 invokePartial: invokePartialWrapper,
95 let ret = templateSpec[i];
96 ret.decorator = templateSpec[i + '_d'];
101 program: function(i, data, declaredBlockParams, blockParams, depths) {
102 let programWrapper = this.programs[i],
104 if (data || depths || blockParams || declaredBlockParams) {
105 programWrapper = wrapProgram(this, i, fn, data, declaredBlockParams, blockParams, depths);
106 } else if (!programWrapper) {
107 programWrapper = this.programs[i] = wrapProgram(this, i, fn);
109 return programWrapper;
112 data: function(value, depth) {
113 while (value && depth--) {
114 value = value._parent;
118 merge: function(param, common) {
119 let obj = param || common;
121 if (param && common && (param !== common)) {
122 obj = Utils.extend({}, common, param);
129 compilerInfo: templateSpec.compiler
132 function ret(context, options = {}) {
133 let data = options.data;
136 if (!options.partial && templateSpec.useData) {
137 data = initData(context, data);
140 blockParams = templateSpec.useBlockParams ? [] : undefined;
141 if (templateSpec.useDepths) {
142 if (options.depths) {
143 depths = context != options.depths[0] ? [context].concat(options.depths) : options.depths;
149 function main(context/*, options*/) {
150 return '' + templateSpec.main(container, context, container.helpers, container.partials, data, blockParams, depths);
152 main = executeDecorators(templateSpec.main, main, container, options.depths || [], data, blockParams);
153 return main(context, options);
157 ret._setup = function(options) {
158 if (!options.partial) {
159 container.helpers = container.merge(options.helpers, env.helpers);
161 if (templateSpec.usePartial) {
162 container.partials = container.merge(options.partials, env.partials);
164 if (templateSpec.usePartial || templateSpec.useDecorators) {
165 container.decorators = container.merge(options.decorators, env.decorators);
168 container.helpers = options.helpers;
169 container.partials = options.partials;
170 container.decorators = options.decorators;
174 ret._child = function(i, data, blockParams, depths) {
175 if (templateSpec.useBlockParams && !blockParams) {
176 throw new Exception('must pass block params');
178 if (templateSpec.useDepths && !depths) {
179 throw new Exception('must pass parent depths');
182 return wrapProgram(container, i, templateSpec[i], data, 0, blockParams, depths);
187 export function wrapProgram(container, i, fn, data, declaredBlockParams, blockParams, depths) {
188 function prog(context, options = {}) {
189 let currentDepths = depths;
190 if (depths && context != depths[0]) {
191 currentDepths = [context].concat(depths);
196 container.helpers, container.partials,
197 options.data || data,
198 blockParams && [options.blockParams].concat(blockParams),
202 prog = executeDecorators(fn, prog, container, depths, data, blockParams);
205 prog.depth = depths ? depths.length : 0;
206 prog.blockParams = declaredBlockParams || 0;
210 export function resolvePartial(partial, context, options) {
212 if (options.name === '@partial-block') {
213 let data = options.data;
214 while (data['partial-block'] === noop) {
217 partial = data['partial-block'];
218 data['partial-block'] = noop;
220 partial = options.partials[options.name];
222 } else if (!partial.call && !options.name) {
223 // This is a dynamic partial that returned a string
224 options.name = partial;
225 partial = options.partials[partial];
230 export function invokePartial(partial, context, options) {
231 options.partial = true;
233 options.data.contextPath = options.ids[0] || options.data.contextPath;
237 if (options.fn && options.fn !== noop) {
238 options.data = createFrame(options.data);
239 partialBlock = options.data['partial-block'] = options.fn;
241 if (partialBlock.partials) {
242 options.partials = Utils.extend({}, options.partials, partialBlock.partials);
246 if (partial === undefined && partialBlock) {
247 partial = partialBlock;
250 if (partial === undefined) {
251 throw new Exception('The partial ' + options.name + ' could not be found');
252 } else if (partial instanceof Function) {
253 return partial(context, options);
257 export function noop() { return ''; }
259 function initData(context, data) {
260 if (!data || !('root' in data)) {
261 data = data ? createFrame(data) : {};
267 function executeDecorators(fn, prog, container, depths, data, blockParams) {
270 prog = fn.decorator(prog, props, container, depths && depths[0], data, blockParams, depths);
271 Utils.extend(prog, props);