emberjs / emberjs/rfcs

Sugar for multi-assignment with `let` block helper

Open
#602 13 comments 3 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
801
Forks
409
PR merge metrics
No merged PRs in 30d

Description

Using block `let` to assign multiple template variables quickly becomes a pain to read the more variables you want to assign, especially when you're assigning values derived from more complex expressions:

```hbs
{{#let (if cond valueA valueB) (some-helper arg1 arg2) (hash a=1 b=2) as |var1 var2 var3|}}
{{var1}}
{{var2}}
{{var3}}
{{/let}}
```

This is already pretty painful to read and it's a simple toy example.

IMO the *reason* why this is painful to read is because there's cognitive overhead required to positionally map the values with the variable they're being assigned to. To fix this, I've seen the following pattern used:

```hbs
{{#let (hash
var1=(if cond valueA valueB)
var2=(some-helper arg1 arg2)
var3=(hash a=1 b=2))
as |vars|}}
{{vars.var1}}
{{vars.var2}}
{{vars.var3}}
{{/let}}
```

This fixes the mental overhead of positional mapping, but it introduces the overhead of naming the hash of the variables as well as the overhead of having to grab those variables off the hash everywhere.

I propose the following syntactic sugar for multi-assignment with block `let`:

```hbs
{{#let
(if cond valueA valueB) as |var1|
(some-helper arg1 arg2) as |var2|
(hash a=1 b=2) as |var3|}}
{{var1}}
{{var2}}
{{var3}}
{{/let}}
```

This solves both of the issues with the current methods of multi-assignment: no mental positional mapping required and no hash overhead.

Another alternative proposal that solves the current issues with block `let` multi-assignment is the inline `let` proposal (as implemented in [ember-let](https://github.com/thefrontside/ember-let)). As discussed [here](https://github.com/emberjs/rfcs/issues/520), adding inline `let` isn't as straight forward because it introduces new scoping semantics into the templating language, which may or may not be desirable. The advantage of the above syntactic sugar solution is that the semantics of the language remain the same.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.