Skip to content

Sugar for multi-assignment with let block helper #602

@sclaxton

Description

@sclaxton

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:

  {{#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:

  {{#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:

  {{#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). As discussed here, 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions