question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Supporting Handlebars subexpressions

See original GitHub issue

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:

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:open
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tndevcommented, Aug 21, 2016

If you convert it to assert anyway, then I’ll use assert. I just looked around in your tests where you compare to 'true' in a non block helper and adapted that style.

0reactions
jonschlinkertcommented, Aug 21, 2016

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found