mirror of
https://github.com/Mibew/mibew.git
synced 2025-03-02 18:08:32 +03:00
Add "ifEqual" Handlebars.js helper
This commit is contained in:
parent
55a33cf012
commit
3f3abb4255
@ -193,4 +193,25 @@
|
|||||||
// All values are "falsy". Render the negative block.
|
// All values are "falsy". Render the negative block.
|
||||||
return options.inverse(this);
|
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);
|
})(Mibew, Handlebars);
|
@ -95,3 +95,27 @@ test('ifAny', function() {
|
|||||||
'Test more than one true values'
|
'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'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user