Make deep @data trigger the data option

This commit is contained in:
Yehuda Katz
2012-07-05 23:05:55 -07:00
parent 5e8be14d78
commit 46c04fa71e
2 changed files with 29 additions and 7 deletions
+4 -2
View File
@@ -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('');
+25 -5
View File
@@ -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;