Add "ifEqual" Handlebars.js helper

This commit is contained in:
Dmitriy Simushev 2014-09-11 09:29:36 +00:00
parent 55a33cf012
commit 3f3abb4255
2 changed files with 45 additions and 0 deletions

View File

@ -193,4 +193,25 @@
// All values are "falsy". Render the negative block.
return options.inverse(this);
});
/**
* Registers "ifEqual" helper.
*
* This helper checks if two values are equal or not. Example of usage:
* <code>
* {{#ifEqual first second}}
* The first argument is equal to the second one.
* {{else}}
* The arguments are not equal.
* {{/ifEqual}}
* </code>
*/
Handlebars.registerHelper('ifEqual', function(left, right, options) {
// Not strict equality is used intentionally here.
if (left == right) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
})(Mibew, Handlebars);

View File

@ -95,3 +95,27 @@ test('ifAny', function() {
'Test more than one true values'
);
});
// Test "ifEqual" Handlebars helper
test('ifEqual', function() {
var template = '{{#ifEqual left right}}true{{else}}false{{/ifEqual}}';
var compiledTemplate = Handlebars.compile(template);
equal(
compiledTemplate({left: 12, right: "foo"}),
'false',
'Test different values'
);
equal(
compiledTemplate({left: "10", right: 10}),
'true',
'Test equal values with different types'
);
equal(
compiledTemplate({left: "Bar", right: "Bar"}),
'true',
'Test equal values'
);
});