summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/compiler.js
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2011-10-20 14:52:21 -0700
committerkpdecker <kpdecker@gmail.com>2011-10-21 08:24:55 -0500
commit4458b5fa133122804888f2454a5f319965adc018 (patch)
treecb3a33f047de456895687836980da46b005bb5c6 /lib/handlebars/compiler/compiler.js
parentb832c85923055d81dbb85cfb52e7b9d9640e0482 (diff)
downloadhandlebars.js-4458b5fa133122804888f2454a5f319965adc018.zip
handlebars.js-4458b5fa133122804888f2454a5f319965adc018.tar.gz
handlebars.js-4458b5fa133122804888f2454a5f319965adc018.tar.bz2
Defer compilation of templates until needed.
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r--lib/handlebars/compiler/compiler.js20
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index 0b4c23d..d4ee32c 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -753,13 +753,19 @@ Handlebars.precompile = function(string, options) {
return new Handlebars.JavaScriptCompiler().compile(environment, options);
};
-Handlebars.compile = function(string, options) {
- options = options || {};
-
- 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);
+Handlebars.compile = function(string, compileOptions) {
+ var compiled;
+ compileOptions = compileOptions || {};
+
+ 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)