diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/handlebars/base.js | 6 | ||||
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 35 |
2 files changed, 29 insertions, 12 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index c51dafb..60c2fcf 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -1,7 +1,7 @@ // BEGIN(BROWSER) var Handlebars = {}; -Handlebars.VERSION = "1.0.beta.2"; +Handlebars.VERSION = "1.0.beta.4"; Handlebars.helpers = {}; Handlebars.partials = {}; @@ -86,6 +86,10 @@ Handlebars.registerHelper('with', function(context, options) { return options.fn(context); }); +Handlebars.registerHelper('log', function(context) { + Handlebars.log(context); +}); + // END(BROWSER) module.exports = Handlebars; diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index c7d051e..039a34e 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -104,7 +104,8 @@ Handlebars.JavaScriptCompiler = function() {}; 'each': true, 'if': true, 'unless': true, - 'with': true + 'with': true, + 'log': true }; if (knownHelpers) { for (var name in knownHelpers) { @@ -335,6 +336,8 @@ Handlebars.JavaScriptCompiler = function() {}; initializeBuffer: function() { return this.quotedString(""); }, + + namespace: "Handlebars", // END PUBLIC API compile: function(environment, options, context, asObject) { @@ -405,8 +408,9 @@ Handlebars.JavaScriptCompiler = function() {}; var out = []; if (!this.isChild) { - var copies = "helpers = helpers || Handlebars.helpers;"; - if(this.environment.usePartial) { copies = copies + " partials = partials || Handlebars.partials;"; } + var namespace = this.namespace; + var copies = "helpers = helpers || " + namespace + ".helpers;"; + if(this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; } out.push(copies); } else { out.push(''); @@ -461,8 +465,6 @@ Handlebars.JavaScriptCompiler = function() {}; params.push("depth" + this.environment.depths.list[i]); } - if(params.length === 4 && !this.environment.usePartial) { params.pop(); } - if (asObject) { params.push(this.source.join("\n ")); @@ -522,7 +524,7 @@ Handlebars.JavaScriptCompiler = function() {}; + " || " + this.nameLookup('depth' + this.lastContext, name, 'context'); } - + toPush += ';'; this.source.push(toPush); } else { @@ -727,7 +729,7 @@ Handlebars.JavaScriptCompiler = function() {}; }; var reservedWords = ("break case catch continue default delete do else finally " + - "for function if in instanceof new return switch this throw " + + "for function if in instanceof new return switch this throw " + "try typeof var void while with null true false").split(" "); var compilerWords = JavaScriptCompiler.RESERVED_WORDS = {}; @@ -756,10 +758,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) |