summaryrefslogtreecommitdiffstats
path: root/spec/qunit_spec.js
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2012-07-05 22:43:05 -0700
committerYehuda Katz <wycats@gmail.com>2012-07-05 22:43:05 -0700
commit72e05d623c07cc3a812528d52fb6d325134efee4 (patch)
tree8eec0d2b12cb9faaeea22a76fd0c045457669f7c /spec/qunit_spec.js
parentff1547ea049e5b4b499dfa8d4d450567a91f5505 (diff)
downloadhandlebars.js-72e05d623c07cc3a812528d52fb6d325134efee4.zip
handlebars.js-72e05d623c07cc3a812528d52fb6d325134efee4.tar.gz
handlebars.js-72e05d623c07cc3a812528d52fb6d325134efee4.tar.bz2
Add support for @data variables
Diffstat (limited to 'spec/qunit_spec.js')
-rw-r--r--spec/qunit_spec.js57
1 files changed, 56 insertions, 1 deletions
diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js
index 5c53e97..1bf831b 100644
--- a/spec/qunit_spec.js
+++ b/spec/qunit_spec.js
@@ -616,7 +616,7 @@ test("if with function argument", function() {
});
test("each", function() {
- var string = "{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!"
+ var string = "{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
shouldCompileTo(string, hash, "goodbye! Goodbye! GOODBYE! cruel world!",
"each with array argument iterates over the contents when not empty");
@@ -624,6 +624,16 @@ test("each", function() {
"each with array argument ignores the contents when empty");
});
+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: {} });
+
+ equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used");
+});
+
test("log", function() {
var string = "{{log blah}}"
var hash = { blah: "whee" };
@@ -655,6 +665,51 @@ test("passing in data to a compiled function that expects data - works with help
equals("happy cat", result, "Data output by helper");
});
+test("data can be looked up via @foo", function() {
+ var template = CompilerContext.compile("{{@hello}}", { data: true });
+ var result = template({}, { data: { hello: "hello" } });
+ equals("hello", result, "@foo retrieves template data");
+});
+
+test("parameter data can be looked up via @foo", function() {
+ var template = CompilerContext.compile("{{hello @world}}", { data: true });
+ var helpers = {
+ hello: function(noun) {
+ return "Hello " + noun;
+ }
+ };
+
+ var result = template({}, { helpers: helpers, data: { world: "world" } });
+ equals("Hello world", result, "@foo as a parameter retrieves template data");
+});
+
+test("hash values can be looked up via @foo", function() {
+ var template = CompilerContext.compile("{{hello noun=@world}}", { data: true });
+ var helpers = {
+ hello: function(options) {
+ return "Hello " + options.hash.noun;
+ }
+ };
+
+ var result = template({}, { helpers: helpers, data: { world: "world" } });
+ equals("Hello world", result, "@foo as a parameter retrieves template data");
+});
+
+test("data is inherited downstream", function() {
+ var template = CompilerContext.compile("{{#let foo=bar.baz}}{{@foo}}{{/let}}", { data: true });
+ var helpers = {
+ let: function(options) {
+ for (var prop in options.hash) {
+ options.data[prop] = options.hash[prop];
+ }
+ return options.fn(this);
+ }
+ };
+
+ var result = template({ bar: { baz: "hello world" } }, { helpers: helpers, data: {} });
+ equals("hello world", result, "data variables are inherited downstream");
+});
+
test("passing in data to a compiled function that expects data - works with helpers in partials", function() {
var template = CompilerContext.compile("{{>my_partial}}", {data: true});