Skip to content
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

Feature Request: new template behavior: switch statements #584

Closed
NullVoxPopuli opened this issue Jan 24, 2020 · 10 comments
Closed

Feature Request: new template behavior: switch statements #584

NullVoxPopuli opened this issue Jan 24, 2020 · 10 comments

Comments

@NullVoxPopuli
Copy link
Sponsor Contributor

NullVoxPopuli commented Jan 24, 2020

Possible usage:

{{#case this.property}}
  {{when "foo"}}  
    No fall-through, because having a {{break}} helper would be weird, imo.
  {{when "bar"}}

  {{when "baz"}}
    boop
{{/case}}

this could probably be implemented as an addon, actually, so I don't know how valuable this would be:

{{#switch this.property as |s|}}
  {{#s.when "foo"}}  

  {{/s.when}}

  {{#s.when "bar"}}

  {{/s.when}}
{{/switch}}

but then, as @miguelcobain mentioned, it'd be possible to render outside of the when blocks, which we wouldn't want.

@nickschot
Copy link

Wouldn't it make more sense to call it "case" instead of "when" to be consistent with the language?

@NullVoxPopuli
Copy link
Sponsor Contributor Author

what language? :)

@nickschot
Copy link

Javascript (and most other programming languages?)

@miguelcobain
Copy link
Contributor

miguelcobain commented Jan 24, 2020

This syntax allows rendering things inside the switch but outside the whens.
This alternative, something that resembles {{#if}} {{else}} {{/if}} wouldn't have that "problem":

{{#switch this.property}}

{{when "foo"}}

{{when "bar"}}

{{else}}
  {{!-- a catch all case? --}}
{{/switch}}

Edit: on a second thought, it's not clear how to handle the first when/case. Perhaps:

{{#switch this.property when "foo"}}

{{when "bar"}}

{{else}}
  {{!-- a catch all case? --}}
{{/switch}}

¯_(ツ)_/¯

@NullVoxPopuli
Copy link
Sponsor Contributor Author

https://www.rubyguides.com/2015/10/ruby-case/
idk

    case capacity
    when 0
      "You ran out of gas."
    when 1..20
      "The tank is almost empty. Quickly, find a gas station!"
    when 21..70
      "You should be ok for now."
    when 71..100
      "The tank is almost full."
    else
      "Error: capacity has an invalid value (#{capacity})"
    end

@webark
Copy link

webark commented Jan 24, 2020

is this the point we start having the jsx conversation? 🤪😇🤓

@NullVoxPopuli
Copy link
Sponsor Contributor Author

in JSX, it's super easy to have unmaintainable code.

restrictions are beautiful ❤️ ✨ 🎉

@miguelcobain
Copy link
Contributor

With the possibility of truth helpers landing in core, I'm not sure if it's worth a switch statement:

{{#if (eq this.property "foo")}}

{{else if (eq this.property "bar")}}

{{else}}

{{/if}}

Sure, a little bit more verbose and maybe more error prone because of the this.property repetition, but still not awful.

@kpfefferle
Copy link
Sponsor

Considering that Ember is a JavaScript framework, I would think we might favor the JS terminology of switch with case statements inside: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

Note that switch was suggested as a built-in helper for Handlebars at one point (and then rejected as not-lightweight-enough).

I'd really like to see an addon that adds this as a Handlebars helper (the above Handlebars issue has some examples), but I'm not convinced it should be part of the core framework. I would definitely use it in some projects (I found the above link when looking for a switch helper just this week).

@NullVoxPopuli
Copy link
Sponsor Contributor Author

we could do an addon that does a build time transform that converts switch to ifs. I think that'd be neat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants