diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 583f88c2..63aa1d60 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -106,10 +106,11 @@ Handlebars.logger = { // override in the host environment log: function(level, str) {} -} +}; Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); }; // END(BROWSER) module.exports = Handlebars; + diff --git a/lib/handlebars/vm.js b/lib/handlebars/vm.js index 5013b7ae..fee59534 100644 --- a/lib/handlebars/vm.js +++ b/lib/handlebars/vm.js @@ -246,8 +246,28 @@ Handlebars.JavaScriptCompiler = function() {}; }; JavaScriptCompiler.prototype = { - compile: function(environment) { + // PUBLIC API: You can override these methods in a subclass to provide + // alternative compiled forms for name lookup and buffering semantics + nameLookup: function(parent, name, type) { + if(JavaScriptCompiler.RESERVED_WORDS[name]) { + return parent + "['" + name + "']"; + } else { + return parent + "." + name; + } + }, + + appendToBuffer: function(string) { + return "buffer = buffer + " + string + ";"; + }, + + initializeBuffer: function() { + return this.quotedString(""); + }, + // END PUBLIC API + + compile: function(environment, data) { this.environment = environment; + this.data = data; this.preamble(); @@ -255,7 +275,7 @@ Handlebars.JavaScriptCompiler = function() {}; this.stackVars = []; this.registers = {list: []}; - this.compileChildren(environment); + this.compileChildren(environment, data); Handlebars.log(Handlebars.logger.DEBUG, environment.disassemble() + "\n\n"); @@ -306,7 +326,7 @@ Handlebars.JavaScriptCompiler = function() {}; preamble: function() { var out = []; - out.push("var buffer = '', currentContext = context"); + out.push("var buffer = " + this.initializeBuffer() + ", currentContext = context"); var copies = "helpers = helpers || Handlebars.helpers;"; if(this.environment.usePartial) { copies = copies + " partials = partials || Handlebars.partials;"; } @@ -323,9 +343,11 @@ Handlebars.JavaScriptCompiler = function() {}; escapeExpression: Handlebars.Utils.escapeExpression, invokePartial: Handlebars.VM.invokePartial, programs: [], - program: function(i, helpers, partials) { + program: function(i, helpers, partials, data) { var programWrapper = this.programs[i]; - if(programWrapper) { + if(data) { + return Handlebars.VM.program(this.children[i], helpers, partials, data); + } else if(programWrapper) { return programWrapper; } else { programWrapper = this.programs[i] = Handlebars.VM.program(this.children[i], helpers, partials); @@ -347,11 +369,14 @@ Handlebars.JavaScriptCompiler = function() {}; var params = ["Handlebars", "context", "helpers", "partials"]; + if(this.data) { params.push("data"); } + for(var i=0, l=this.environment.depths.list.length; i