diff --git a/src/mibew/js/source/default/handlebars_helpers.js b/src/mibew/js/source/default/handlebars_helpers.js index 4cc14c54..3e85109c 100644 --- a/src/mibew/js/source/default/handlebars_helpers.js +++ b/src/mibew/js/source/default/handlebars_helpers.js @@ -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: + * + * {{#ifEqual first second}} + * The first argument is equal to the second one. + * {{else}} + * The arguments are not equal. + * {{/ifEqual}} + * + */ + 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); \ No newline at end of file diff --git a/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js b/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js index 990ef6b0..d3fd7ea7 100644 --- a/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js +++ b/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js @@ -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' + ); +});