Skip to content

Commit

Permalink
Merge pull request #569 from wycats/lookup-helper
Browse files Browse the repository at this point in the history
Unable to lookup array values using @index
  • Loading branch information
kpdecker committed Feb 9, 2014
2 parents a9f76e1 + 306feb4 commit fcec69a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/handlebars/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
3 changes: 2 additions & 1 deletion lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions spec/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
});
});
});

0 comments on commit fcec69a

Please sign in to comment.