Implement lookup helper

This commit is contained in:
kpdecker
2014-02-09 17:22:18 -06:00
parent a9f76e1475
commit 306feb497c
3 changed files with 27 additions and 1 deletions
+4
View File
@@ -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 = {
+2 -1
View File
@@ -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) {
+21
View File
@@ -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, '');
});
});
});