Template string interpolation
- Dominant language
- No language data
- Stars
- 801
- Forks
- 409
- PR merge metrics
- No merged PRs in 30d
Description
Just some quick notes for the idea to open up the discussion.
It's not very common, but occasionally you need to do string interpolations in Handlebars. Today, the solution is to use the `concat` helper.
```hbs
{{notification message=(concat @user.firstName " " @user.lastName " has added you as a friend!")}}
```
It's not so bad, but it does add some noise. It would be nicer, in my opinion, to have a string interpolation syntax.
Ideally, it would be nice if we could just use the normal string syntax:
```hbs
{{notification message="{{@user.firstName}} {{@user.lastName}} has added you as a friend!"}}
```
Note that this already works in angle bracket invocations, so it aligns nicely:
```hbs
```
This would work if we are designing the language from scratch. You can always escape the mustaches if you meant for them to be literal mustaches, same as any other positions. Unfortunately, this will be a breaking change. While rare, it is possible that there are existing string literals that uses mustaches for this purpose.
The good thing about this is they are by definition static. So one option is to detect existing usages of unescaped mustaches in string literals, deprecate them to clear the space, then add the interpolation feature in the next major version. In the meantime, we can create an addon for apps to opt-in.
Another option is to do what JavaScript (which had the same problem and didn't have the luxury of bumping major versions) and use a different syntax for interpolated strings, say backticks:
```hbs
{{notification message=`{{@user.firstName}} {{@user.lastName}} has added you as a friend!`}}
```
Personally, I would prefer to do the former, as it feels more consistent overall.
In terms of implementation, ideally this would be supported in the handlebars parser directly, but if not, we can re-parse string literal AST node in our pwn parser. Because it already "works" today (kind of by definition, because you can put anything in a string literal), I don't think it will cause any technical parsing challenge/ambiguities. But this is all just a quick sketch so I could be missing something.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.