diff options
author | Peter Wagenet <peter.wagenet@gmail.com> | 2011-10-24 17:27:54 -0700 |
---|---|---|
committer | Peter Wagenet <peter.wagenet@gmail.com> | 2011-10-24 17:27:54 -0700 |
commit | 374a38ed36e8dad693c466cf104067cfd6acd589 (patch) | |
tree | 93ec85523a93e089ec9cb57e5302858ad9e6707a /lib/handlebars/compiler/compiler.js | |
parent | b501c380af6cdefc308ea23069b9e0db3237f4c5 (diff) | |
parent | defc2f340378b4739cd715a7468c30b068db7793 (diff) | |
download | handlebars.js-374a38ed36e8dad693c466cf104067cfd6acd589.zip handlebars.js-374a38ed36e8dad693c466cf104067cfd6acd589.tar.gz handlebars.js-374a38ed36e8dad693c466cf104067cfd6acd589.tar.bz2 |
Merge pull request #130 from kpdecker/defer-compile
Defer compile
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 0b4c23d..7b759c1 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -756,10 +756,21 @@ Handlebars.precompile = function(string, 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); + var compiled; + function compile() { + 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); + } + + // Template is only compiled on first use and cached after that point. + return function(context, options) { + if (!compiled) { + compiled = compile(); + } + return compiled.call(this, context, options); + }; }; // END(BROWSER) |