-
-
Notifications
You must be signed in to change notification settings - Fork 408
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
Handlebars syntax for the (get) helper #379
Comments
This situation gets a bit more complicated if we include some allowances for subexpressions: {{!-- How would these be represented with the dynamic access syntax? --}}
{{get (planets-near-star star) name}}
{{get planets (favourite-planet-name user)}} I'd suggest {{(planets-near-star star)[name]}}
{{planets[(favourite-planet-name user)]}} |
Is this form allowed/reasonable? {{planets[name[closestPlanet[field]]]}} |
@nightire Yeah. I'd expect the grammar to be something as simple and generic as
|
I think we can PoC or actually properly implement this as a template transform addon to avoid changing handlebars itself. |
@buschtoens Unfortunately, |
How far of a difference between http://handlebarsjs.com/ and Ember's flavor are we at this point? In that, is this something that should be requested to be added to the official Handlebars.js project, or is Ember beyond that point? Also, adoption of editor syntax highlighters would probably be better if added to the official Handlebars.js project. |
@Panman8201 - AFAICT @mmun is suggesting adding this to the main handlebars parser (which we would then update to in glimmer-vm)... |
@mmun - I'm sold! 😸 |
@rwjblue Guess I was questioning it because this place is for Ember RFC's. Does Handlebars.js have their own RFC process this should go through? |
This introduces a very different syntax than what we currently have, as we don't have anything with Since it's solving more of an edge case, I don't think it should be added as it would add to the list of things needed learn to use ember. |
If we did not add this syntax, users would still have to learn about the I would argue that most Ember new-comers never worked with Handlebars before. And I can tell from my own and co-worker's experience, that most seem to intuitively expect the So actually, I think this decreases the learning curve. 😊 |
@buschtoens yes, but the users are learning about helpers anyway, so that example of yours ( |
@btecu Would So far, |
@buschtoens I get it, since I already read this RFC.
Exactly my point, this is not React to have JS in the template. 😃 I'm not worried about |
I will give a kidney for this feature. no bamboozle |
Adding this seems like a step away from the "lisp-ish" syntax that Handlebars/Ember templates tout as a feature. What would prevent making a case for something like |
Question: is there any consideration here in regard to ever wanting to use |
@richard-viney Do you mean creating an array on the fly? {{if (contains needle ["foo", "bar"]) "found it!"}}
instead of
{{if (contains needle (array "foo" "bar")) "found it!"}} |
@buschtoens I was meaning simple index lookup, i.e. exactly the same as in JS: |
@richard-viney Ah, I understand. 😊 |
I'm personally a bit ambivalent on this, as despite headaches that we run into now and then (particularly in deeper loops), part of what I appreciate about Handlebars syntax at the moment is that it constrains access to things and helps keep things relatively simple. At the moment, despite some pretty sophisticated and complex helpers, as a community we still seem to value simpler templates with more complex logic in components. This would seem to be a shift away from that ... |
Aren't expressions in handlebars supposed to be kept as simple as possible ? Based on the original example, anything like that can easily moved to an |
I think that people are right to be nervous about this. I am in favor of this particular syntax change, but only after carefully evaluating the Handlebars and the Glimmer grammar, and working through the reasons that this syntax is less disruptive than it looks. This analysis also gives us a way to evaluate future syntax extensions. Adjacent ExpressionsOne of the most constraining syntax choices of Handlebars was the decision to leave out commas between expressions. This means that there are many places in the grammar where two expressions are literally adjacent to each other. {{t "hello" "world"}} In this example, we are calling the But not all expressions are simple literals. Let's take a look at paths: {{t my.name is.wycats}} In this example, we are calling the The general rule here is that if it is possible to continue a complete expression with any token ( For the rest of this comment, the term continuation token is defined to mean a token that is permitted to follow a valid expression to produce a longer valid expression. The term start token is defined to mean a token that is valid at the beginning of an expression. The rule we just described can be summarized as: in Handlebars, a token cannot be both a continuation token and a start token.
Other LanguagesHandlebars has an extremely restrictive grammar, but let's take a look at JavaScript. In JavaScript Let's say we tried to allow both of these syntaxes in Handlebars: {{t x + y}} Superficially, this looks fine. It's "clearly" a call to the helper But what about this: {{t x + y + z}} This is ambiguous: it might mean a call to The general analysis from above applies: because Note that JavaScript carefully avoids this problem by carefully avoiding adjacent expressions. Whenever two expressions are placed side by side in a list, there is always some kind of separator that is not a start token (usually Ambiguity in HandlebarsThis analysis is a big part of what keeps the Handlebars grammar simple. Because we allow adjacent expressions, we are prevented from casually adding infix or unary operators. This is a property that people like a lot about the Handlebars template language, and it's nice that this analysis shows us that it would be hard to go too far down the slippery slope for a simple technical reason. About the
|
P.S. This analysis also illustrates why we can't add Today, Here's the ambiguity: {{t hello(world)}} Does |
@wycats that was a really great read on how you think about this, thanks for posting. That said, I'm not sure your explanation addresses the general concern of adding syntax to Handlebars. (For me, it actually added an additional concern of would we ever want To re-iterate, it's great to know that adding a To further illustrate this ambiguity in context of your post, there are other, unused characters that can still be introduced as continuation or start tokens. For example, if This puts us in the situation where we (1) have to make hard decisions about continuation vs start tokens and (2) move closer to JS syntax without being able to support all of it, which ends up being a bad experience and also moves an arbitrary line for existing users and new users. In my opinion, it's not a huge win to add this behavior at least until Footnotes
|
The lisp syntax is already a bad experience. Doesn't matter how many years I use it for it sucks. Still better than alternatives mixing logic into HTML syntax. Handlebars would be perfect if it didn't have such hardcore restrictions from the start because of some philosophy about logic in controllers or something. Easily my number 1 issue with using ember. Hell you mix components and helpers as the exact same syntax (except 1 can be block)... hows that for your ambiguity. |
Some of the restrictions are technical, some of them are philosophical. I'd say the philosophical restrictions are largely handled by 3rd party addons.
That's changing pretty soon with angle bracket components
¯_(ツ)_/¯ I don't mind it so much, but that's neither here nor there. |
@wycats sorry if I'm wrong, but wouldn't this work by just tokenizing whitespaces as seperators? I feel like So So the first (and the later if whitespaces are not tokenized) would would tokenize to something like this:
However the later (if whitespaces are tokenized) should result in something like this:
So shouldn't a grammar like this work?
|
@luxferresum
Isn't this a massive breaking change?
The Lispiness of Handlebars can be referring to a couple different concepts. In terms of syntax, I think that adding Handlebars is definitely a separate language from JavaScript with its own syntax and semantics. The overlap I think is in how thruthiness and falsiness are handled. If I remember correctly, JavaScript semantics are maintained? I think the bracket accessor syntax is in a couple of syntax, right? So it would overlap with a bunch of host languages.
This doesn't sound like a new situation ;) |
I'm not sure what you mean - what existing syntax/API would break? |
I'm doing some issue gardening 🌱🌿 🌷 and came upon this issue. Since it's quite old I just wanted to ask if this is still relevant? If it isn't, maybe we can close this issue? By closing some old issues we reduce the list of open issues to a more manageable set. |
I'm closing this due to inactivity. This doesn't mean that the idea presented here is invalid, but that, unfortunately, nobody has taken the effort to spearhead it and bring it to completion. Please feel free to advocate for it if you believe that this is still worth pursuing. Thanks! |
With instead of {{get (get (get planets name) 'closestPlanet') field}} you can do const closestPlanet = (planets, name, field) => planets[name].closestPlaned[field];
<template>
{{closestPlanet planets name field}}
assuming planets, name, and field are defined "somewhere"
</template> and even pre-strict mode, and post 3.25, you can do: export default class Demo extends Component {
closestPlanet = (planets, name, field) => planets[name].closestPlaned[field];
} {{this.closestPlanet planets name field}} |
What do you think about introducing a "dynamic access" syntax for Handlebars to streamline or even replace the need for the
get
helper?would be equivalent to
{{get planets name}}
, andwould be equivalent to
The second example clearly demonstrates that using the dynamic access syntax makes it easier to understand what is being accessed.
The text was updated successfully, but these errors were encountered: