baccdb4cfc
Include program id and depth on the generated wrapper objects. This allows helpers who are passed these objects to differentiate between helpers for cases where they may want to cache the generated DOM structure.
97 lines
3.3 KiB
JavaScript
97 lines
3.3 KiB
JavaScript
exports.attach = function(Handlebars) {
|
|
|
|
// 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) {
|
|
programWrapper = Handlebars.VM.program(i, fn, data);
|
|
} else if (!programWrapper) {
|
|
programWrapper = this.programs[i] = Handlebars.VM.program(i, fn);
|
|
}
|
|
return programWrapper;
|
|
},
|
|
programWithDepth: Handlebars.VM.programWithDepth,
|
|
noop: Handlebars.VM.noop,
|
|
compilerInfo: null
|
|
};
|
|
|
|
return function(context, options) {
|
|
options = options || {};
|
|
var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
|
|
|
|
var compilerInfo = container.compilerInfo || [],
|
|
compilerRevision = compilerInfo[0] || 1,
|
|
currentRevision = Handlebars.COMPILER_REVISION;
|
|
|
|
if (compilerRevision !== currentRevision) {
|
|
if (compilerRevision < currentRevision) {
|
|
var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision],
|
|
compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision];
|
|
throw "Template was precompiled with an older version of Handlebars than the current runtime. "+
|
|
"Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").";
|
|
} else {
|
|
// Use the embedded version info since the runtime doesn't know about this revision yet
|
|
throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+
|
|
"Please update your runtime to a newer version ("+compilerInfo[1]+").";
|
|
}
|
|
}
|
|
|
|
return result;
|
|
};
|
|
},
|
|
|
|
programWithDepth: function(i, fn, data /*, $depth */) {
|
|
var args = Array.prototype.slice.call(arguments, 3);
|
|
|
|
var program = function(context, options) {
|
|
options = options || {};
|
|
|
|
return fn.apply(this, [context, options.data || data].concat(args));
|
|
};
|
|
program.program = i;
|
|
program.depth = args.length;
|
|
return program;
|
|
},
|
|
program: function(i, fn, data) {
|
|
var program = function(context, options) {
|
|
options = options || {};
|
|
|
|
return fn(context, options.data || data);
|
|
};
|
|
program.program = i;
|
|
program.depth = 0;
|
|
return program;
|
|
},
|
|
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)
|
|
|
|
return Handlebars;
|
|
|
|
};
|