-
Notifications
You must be signed in to change notification settings - Fork 2k
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
functions in nested objects are not bound to that object, resulting in inconsistent behavior #1860
Comments
This doesn't seem to be a bug but wrong expectations:
This outputs Handlebars doesn't make a difference between a registered helper function or a function from within the context. The context of the function is always bound to where you call it from. |
Sure, but in this case we are talking specifically about a Javascript feature and the Javascript Handlebars library. We are talking specifically about how Javascript implemented contexts with Javascript functions as properties in that context behave. It stands to reason that such functions behave in a way that is consistent with and natural to Javascript. Much more importantly, it also stands to reason, from the template author's side, without even knowing that the context is implemented in Javascript or even that the {{#with nested}}
{{awesome}}
{{/with}} and {{nested.awesome}} yield the exact same thing, just as if I would expect this consistency from any other Handlebars library, be it Python, Rust, Golang or any other language implementations. Perhaps I should update the title of this issue. |
@jaylinski, @ilharp, or @nknapp, if you confirm that this is a bug that should be fixed, I could take a shot at a PR. Does the label indicate that? |
@PitPik has some valid points in his response, I'm not sure if this is really a bug, but wrong expectations. This behavior should be defined as part of a specification. (But so far all attempts to write a specification failed: https://github.com/handlebars-lang/spec, https://github.com/handlebars-lang/specification) |
@jaylinski Kinda sounds like you're saying "it is what it is". Is it because the various implementations are inconsistent and also too entrenched to impose any consistency? Which points of @PitPik are valid? Are none of mine? Honest question. |
In this case Handlebars mimics the behavior of Mustache. There is no This fiddle demonstrates it: https://jsfiddle.net/ob5ezgcn/6/
I am not sure if it is specified in the Mustache spec explicitly. Personally, I would advise against the use of functions in the context. This feature is a relict due to the compatibility with Mustache. Use a helper instead. Helper-behavior is much more constent. In my eyes, the primary use case of Handlebars is rendering JSON-like objects. |
Given the following input:
the following template:
yields
'More awesome'
, whereas the equivalent Javascript:yields
'Deeply awesome'
More importantly,
also yields
'Deeply awesome'
.Here is the js-fiddle demonstrating this
PR submitted with a failing test case: #1861.
The text was updated successfully, but these errors were encountered: