diff options
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 6 | ||||
-rw-r--r-- | spec/qunit_spec.js | 30 |
2 files changed, 29 insertions, 7 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index ae48c69..c7d2dbb 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -216,7 +216,7 @@ Handlebars.JavaScriptCompiler = function() {}; } if (id.type === 'DATA') { - this.opcode('lookupData', id.id); + this.DATA(id); } else if (id.parts.length) { this.opcode('lookupOnContext', id.parts[0]); for(var i=1, l=id.parts.length; i<l; i++) { @@ -252,6 +252,7 @@ Handlebars.JavaScriptCompiler = function() {}; }, DATA: function(data) { + this.options.data = true; this.opcode('lookupData', data.id); }, @@ -446,7 +447,8 @@ Handlebars.JavaScriptCompiler = function() {}; if (!this.isChild) { var namespace = this.namespace; var copies = "helpers = helpers || " + namespace + ".helpers;"; - if(this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; } + if (this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; } + if (this.options.data) { copies = copies + " data = data || {};"; } out.push(copies); } else { out.push(''); diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js index 1bf831b..aca8fcb 100644 --- a/spec/qunit_spec.js +++ b/spec/qunit_spec.js @@ -628,8 +628,8 @@ test("each with @index", function() { var string = "{{#each goodbyes}}{{@index}}. {{text}}! {{/each}}cruel {{world}}!"; var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"}; - var template = CompilerContext.compile(string, {data: true}); - var result = template(hash, { data: {} }); + var template = CompilerContext.compile(string); + var result = template(hash); equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used"); }); @@ -666,13 +666,33 @@ test("passing in data to a compiled function that expects data - works with help }); test("data can be looked up via @foo", function() { - var template = CompilerContext.compile("{{@hello}}", { data: true }); + var template = CompilerContext.compile("{{@hello}}"); var result = template({}, { data: { hello: "hello" } }); equals("hello", result, "@foo retrieves template data"); }); +var objectCreate = Handlebars.createFrame; + +test("deep @foo triggers automatic top-level data", function() { + var template = CompilerContext.compile('{{#let world="world"}}{{#if foo}}{{#if foo}}Hello {{@world}}{{/if}}{{/if}}{{/let}}'); + + var helpers = objectCreate(Handlebars.helpers); + + helpers.let = function(options) { + var frame = Handlebars.createFrame(options.data); + + for (var prop in options.hash) { + frame[prop] = options.hash[prop]; + } + return options.fn(this, { data: frame }); + }; + + var result = template({ foo: true }, { helpers: helpers }); + equals("Hello world", result, "Automatic data was triggered"); +}); + test("parameter data can be looked up via @foo", function() { - var template = CompilerContext.compile("{{hello @world}}", { data: true }); + var template = CompilerContext.compile("{{hello @world}}"); var helpers = { hello: function(noun) { return "Hello " + noun; @@ -684,7 +704,7 @@ test("parameter data can be looked up via @foo", function() { }); test("hash values can be looked up via @foo", function() { - var template = CompilerContext.compile("{{hello noun=@world}}", { data: true }); + var template = CompilerContext.compile("{{hello noun=@world}}"); var helpers = { hello: function(options) { return "Hello " + options.hash.noun; |