diff options
author | Kevin Decker <kpdecker@gmail.com> | 2014-02-09 17:50:28 -0600 |
---|---|---|
committer | Kevin Decker <kpdecker@gmail.com> | 2014-02-09 17:50:28 -0600 |
commit | fcec69ae2c838a9df7a456557b57671a80d1732b (patch) | |
tree | 1163dacb2f41831cca9d1db6fc63da463e25ce43 | |
parent | a9f76e1475e4b74572bbdf6abc07ba7893bf349e (diff) | |
parent | 306feb497c72d539ce0f6f926fc8844fb35844fe (diff) | |
download | handlebars.js-fcec69ae2c838a9df7a456557b57671a80d1732b.zip handlebars.js-fcec69ae2c838a9df7a456557b57671a80d1732b.tar.gz handlebars.js-fcec69ae2c838a9df7a456557b57671a80d1732b.tar.bz2 |
Merge pull request #569 from wycats/lookup-helper
Unable to lookup array values using @index
-rw-r--r-- | lib/handlebars/base.js | 4 | ||||
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 3 | ||||
-rw-r--r-- | spec/builtins.js | 21 |
3 files changed, 27 insertions, 1 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 7ebfc55..02d274d 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -193,6 +193,10 @@ function registerDefaultHelpers(instance) { var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1; instance.log(level, context); }); + + instance.registerHelper('lookup', function(obj, field, options) { + return obj && obj[field]; + }); } export var logger = { diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index cfcc70a..21e1024 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -85,7 +85,8 @@ Compiler.prototype = { 'if': true, 'unless': true, 'with': true, - 'log': true + 'log': true, + 'lookup': true }; if (knownHelpers) { for (var name in knownHelpers) { diff --git a/spec/builtins.js b/spec/builtins.js index 53f46cc..bbe494e 100644 --- a/spec/builtins.js +++ b/spec/builtins.js @@ -203,4 +203,25 @@ describe('builtin helpers', function() { equals("whee", logArg, "should call log with 'whee'"); }); + + describe('#lookup', function() { + it('should lookup arbitrary content', function() { + var string = '{{#each goodbyes}}{{lookup ../data .}}{{/each}}', + hash = {goodbyes: [0, 1], data: ['foo', 'bar']}; + + var template = CompilerContext.compile(string); + var result = template(hash); + + equal(result, 'foobar'); + }); + it('should not fail on undefined value', function() { + var string = '{{#each goodbyes}}{{lookup ../bar .}}{{/each}}', + hash = {goodbyes: [0, 1], data: ['foo', 'bar']}; + + var template = CompilerContext.compile(string); + var result = template(hash); + + equal(result, ''); + }); + }); }); |