Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 26 additions & 3 deletions spec/builtins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global CompilerContext, shouldCompileTo, compileWithPartials */
/*global CompilerContext, shouldCompileTo, compileWithPartials, handlebarsEnv */
describe('builtin helpers', function() {
describe('#if', function() {
it("if", function() {
Expand Down Expand Up @@ -189,16 +189,39 @@ describe('builtin helpers', function() {
});

it("#log", function() {

var string = "{{log blah}}";
var hash = { blah: "whee" };

var levelArg, logArg;
handlebarsEnv.log = function(level, arg){ levelArg = level, logArg = arg; };
handlebarsEnv.log = function(level, arg){
levelArg = level;
logArg = arg;
};

shouldCompileTo(string, hash, "", "log should not display");
equals(1, levelArg, "should call log with 1");
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, '');
});
});
});