Skip to content
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

Subexpressions don't work as expected #56

Closed
halfdan opened this issue Feb 10, 2014 · 15 comments
Closed

Subexpressions don't work as expected #56

halfdan opened this issue Feb 10, 2014 · 15 comments

Comments

@halfdan
Copy link
Contributor

halfdan commented Feb 10, 2014

This is a follow up on #55 but might not directly be related.

Using sub-expressions like {{encode (url)}} (as in https://github.com/halfdan/hbs-weird/) where the url helper returns a URL and encode executes encodeURIComponent the expression is replaced by only the result of the url helper. Using {{encode "http://foo.bar/?baz=muh"}} on its own seems to work just fine.

Example case in https://github.com/halfdan/hbs-weird/

@mgutz
Copy link

mgutz commented Feb 10, 2014

Thanks. I'll look tonight or tomorrow.

@mgutz
Copy link

mgutz commented Feb 11, 2014

I cannot think of a quick solution right now. Looked at it for a couple of hours and I'm drawing a blank.

Here is what is happening

  1. Express-hbs wraps weird into a promise and returns to handlebars a unique token to be replaced later.
  2. (weird) returns a token say zXyt234
  3. Handlebars executes encode(zXyt234) returning zXyt234. Subexpressions cannot be deferred values.
  4. AFTER handlebars has invoked all functions and subsequently all promises resolved, express-hbs replaces __zXyt234 with the result of weird which is the original url.

@ErisDS
Copy link
Member

ErisDS commented Feb 11, 2014

Out of interest does this work if both helpers are async? Or does that cause even more mess?

@halfdan
Copy link
Contributor Author

halfdan commented Feb 12, 2014

When I change encode into an async helper I end up with tokens again:

Unexpected behaviour: __aSyNcId_lhqgvIz___

That's from encode (url) where both, encode and url are async helpers.

@halfdan
Copy link
Contributor Author

halfdan commented Mar 4, 2014

@mgutz Any update on this?

@mgutz
Copy link

mgutz commented Mar 4, 2014

In the process of moving. Will look at this maybe next week.

@ErisDS
Copy link
Member

ErisDS commented Apr 3, 2014

I would like to know if it's even going to be possible to fix this. If not we need to reduce our reliance on async helpers. They're a very useful feature, but if they don't work well together with standard helpers when using subexpressions, then we have a bit of a problem with our API which we will need to resolve some other way.

@mgutz
Copy link

mgutz commented Apr 3, 2014

This is not an easy thing to do; short of forking handlebars to properly handle async helpers, any other solution would be kludgy at best. I'm leaning towards "wont fix".

@ErisDS
Copy link
Member

ErisDS commented Apr 3, 2014

I found this: handlebars-lang/handlebars.js#141

I think if you have an idea of how to implement this inside handlebars that it would gain traction. Ghost is one very clear example of why async helpers aren't just useful but pretty much required.

Do you have any thoughts on how to do it? I'd love to hear your thoughts on how it could be done in handlebars and how it might done in a 'kludgy' way here in express-hbs so that we can see how the best way to move forward is, and whose arm Ghost needs to bend to make it happen 👍

@nknapp
Copy link

nknapp commented Jul 29, 2015

@mgutz, could you have a look at promised-handlebars) I have written it in the past week and I have working test-cases for nesting async helpers within async block-helpers. Though, it has not been tested on larger examples yet.
Any feedback is appreciated.

@mgutz
Copy link

mgutz commented Jul 29, 2015

Will do. I might not be able to look at it for at least several days though.

@nknapp
Copy link

nknapp commented Aug 6, 2015

I think, promised-handlebars now has all the features I would expect from it.
The following cases all work with sync- and async-helpers in all combinations,
where "async" means that the helper returns a promise for the value rather than the value itself.

  • Simple helper calls {{{helper arg}}}
  • HTML-Escaping the result {{helper arg}}
  • Nesting helpers in parameters {{helper (nested-helper)}}
  • Helpers called from within block helpers: {{#block-helper}}{{inner-helper}}{{/block-helper}}
  • Usage with built-in helpers {{#if (nested helper)}}abc{{/if}}
  • Helpers as hash arguments {{helper param=(nested-helper) }}
  • Helpers as arguments of a partial {{>partial (helper arg)}}

@nknapp
Copy link

nknapp commented Nov 15, 2015

@mgutz have you had any chance to look at promised-handlebars yet?

@bradvogel
Copy link

Bump

@jtwebman
Copy link
Collaborator

jtwebman commented Dec 2, 2018

This is fixed now with my latest merge.

@jtwebman jtwebman closed this as completed Dec 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants