Defer compilation of templates until needed.

This commit is contained in:
kpdecker
2011-10-20 14:52:21 -07:00
parent b832c85923
commit 7cd34edcc2
+12 -6
View File
@@ -753,13 +753,19 @@ Handlebars.precompile = function(string, options) {
return new Handlebars.JavaScriptCompiler().compile(environment, options);
};
Handlebars.compile = function(string, options) {
options = options || {};
Handlebars.compile = function(string, compileOptions) {
var compiled;
compileOptions = compileOptions || {};
var ast = Handlebars.parse(string);
var environment = new Handlebars.Compiler().compile(ast, options);
var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
return Handlebars.template(templateSpec);
return function(context, options) {
if (!compiled) {
var ast = Handlebars.parse(string);
var environment = new Handlebars.Compiler().compile(ast, options);
var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
compiled = Handlebars.template(templateSpec);
}
return compiled.call(this, context, options);
};
};
// END(BROWSER)