6fe7f17c89
This check reduces duplicated code as well as also failing if the template was precompiled on a version before the check was added.
74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
var Handlebars = require("./base");
|
|
|
|
// BEGIN(BROWSER)
|
|
Handlebars.VM = {
|
|
template: function(templateSpec) {
|
|
// Just add water
|
|
var container = {
|
|
escapeExpression: Handlebars.Utils.escapeExpression,
|
|
invokePartial: Handlebars.VM.invokePartial,
|
|
programs: [],
|
|
program: function(i, fn, data) {
|
|
var programWrapper = this.programs[i];
|
|
if(data) {
|
|
return Handlebars.VM.program(fn, data);
|
|
} else if(programWrapper) {
|
|
return programWrapper;
|
|
} else {
|
|
programWrapper = this.programs[i] = Handlebars.VM.program(fn);
|
|
return programWrapper;
|
|
}
|
|
},
|
|
programWithDepth: Handlebars.VM.programWithDepth,
|
|
noop: Handlebars.VM.noop,
|
|
compiledVersion: null
|
|
};
|
|
|
|
return function(context, options) {
|
|
options = options || {};
|
|
var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
|
|
if (container.compiledVersion !== Handlebars.VERSION) {
|
|
throw "Template was compiled with "+(container.compiledVersion || 'unknown version')+", but runtime is "+Handlebars.VERSION;
|
|
}
|
|
return result;
|
|
};
|
|
},
|
|
|
|
programWithDepth: function(fn, data, $depth) {
|
|
var args = Array.prototype.slice.call(arguments, 2);
|
|
|
|
return function(context, options) {
|
|
options = options || {};
|
|
|
|
return fn.apply(this, [context, options.data || data].concat(args));
|
|
};
|
|
},
|
|
program: function(fn, data) {
|
|
return function(context, options) {
|
|
options = options || {};
|
|
|
|
return fn(context, options.data || data);
|
|
};
|
|
},
|
|
noop: function() { return ""; },
|
|
invokePartial: function(partial, name, context, helpers, partials, data) {
|
|
var options = { helpers: helpers, partials: partials, data: data };
|
|
|
|
if(partial === undefined) {
|
|
throw new Handlebars.Exception("The partial " + name + " could not be found");
|
|
} else if(partial instanceof Function) {
|
|
return partial(context, options);
|
|
} else if (!Handlebars.compile) {
|
|
throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
|
|
} else {
|
|
partials[name] = Handlebars.compile(partial, {data: data !== undefined});
|
|
return partials[name](context, options);
|
|
}
|
|
}
|
|
};
|
|
|
|
Handlebars.template = Handlebars.VM.template;
|
|
|
|
// END(BROWSER)
|
|
|