-
-
Notifications
You must be signed in to change notification settings - Fork 364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supporting Handlebars subexpressions #224
Comments
@tndev Thanks for the issue! If you're reporting a bug, please be sure to include:
|
it's not related to sub-expressions, the helper is a block helper, and it's being used as a non-block helper. I'm not opposed to adding logic to support both if you want to do a pr with unit tests. |
I would write the tests according to your it('should return true if a > b as a non-block helper.', function() {
hbs.compile('{{gt 20 15}}')().should.eql('true');
});
it('should return false if a < b as a non-block helper.', function() {
hbs.compile('{{gt 14 15}}')().should.eql('false');
});
it('should return false if a == b as a non-block helper.', function() {
hbs.compile('{{gt 14 14}}')().should.eql('false');
}); (the same for all the other comparison helpers) |
I think so, yeah. Feel free to use |
If you convert it to |
Not sure if I should open an new Issue about that in general, discuss it here or if I should add that as discussion about it with the pull request as soon as I have finished the rewriting. While doing the rewriting it seem the code might be clearer and easier to maintain, if the comparison functions would always return module.exports = comparisonHelpers.map(function(helper) {
return function() {
var options = arguments[arguments.length - 1];
var result = helper.apply(this, arguments);
// used as block helper
if (options.fn) {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
} else { // used as non-block helper
return result;
}
};
}); |
I finished the code update for the whole |
@tndev great, I was thinking about this, I like the concept but it would help to see the code so we can discuss. Feel free to do the pr, thx |
I saw this question Handlebars subexpression throws “options.fn is not a function” error on stackoverflow:
The OP was asking why
{{#and (gt 4 3) (gt 5 4)}}OK{{/and}}
will throw a:In the code it is obvious the helper requires
fn
andinverse
(so an if/else) block:To support
{{#and (gt 4 3) (gt 5 4)}}OK{{/and}}
the helper should returntrue
orfalse
instead ofoptions.fn(this)
oroptions.inverse(this)
if those blocks don't exists.This might probably be a interesting feature. Or am I missing something here?
To support subexpressions the code could be changed to this:
The text was updated successfully, but these errors were encountered: