Supporting Handlebars subexpressions
See original GitHub issueI 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:
TypeError: options.fn is not a function
In the code it is obvious the helper requires fn
and inverse
(so an if/else) block:
helpers.gt = function(a, b, options) {
if (arguments.length === 2) {
options = b;
b = options.hash.compare;
}
if (a > b) {
return options.fn(this);
}
return options.inverse(this);
};
To support {{#and (gt 4 3) (gt 5 4)}}OK{{/and}}
the helper should return true
or false
instead of options.fn(this)
or options.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:
helpers.gt = function(a, b, options) {
if (arguments.length === 2) {
options = b;
b = options.hash.compare;
}
//fn block exists: it is not a subexpression
if( options.fn ) {
if (a > b) {
return options.fn(this);
}
return options.inverse(this);
} else { // otherwise return the result of the comparison
return a > b;
}
};
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Subexpressions in Handlebars - Stack Overflow
If you're not using that version, the workaround is to place the helper inside a {{#with}} block and remove the path notation from...
Read more >Handlebars: Subexpression
Handlebars : Subexpression ... Subexpressions allow you to invoke multiple helpers inside a tag. The result of inner helper will be arguments of...
Read more >Handlebars - Helpers as expressions ( Subexpressions )
1. script#handlebars_template( type = "text/x-handlebars-template" ) ; 2. | {{#strictlyEquals ( concat this.left this.right ) this.expected}} ; 3. | <div class=" ...
Read more >handlebars-subexpression-helpers - npm
Some simple yet useful subexpression helpers for handlebars. Latest version: 1.0.9, last published: 6 years ago.
Read more >Minimal Templating on Steroids - Handlebars.js (archive)
Handlebars offers support for subexpressions, which allows you to invoke multiple helpers within a single mustache, and pass in the results of inner...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you convert it to
assert
anyway, then I’ll useassert
. I just looked around in your tests where you compare to'true'
in a non block helper and adapted that style.@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