Switch / Case blocks for templates
See original GitHub issueI’ve had a couple times where I’ve missed a switch
equivalent for logic in templates. I have been using long if-elseif chains, but this can get ugly. It’s mostly useful for when you want to display different components based on an enum. Haven’t seen any other issues about this, but switch statements are commonly used for control flow and I was a bit suprised they were absent in Svelte.
{#switch type}
{:case 'loading'}
<Loading />
{:case 'success'}
<Content />
{:case 'timeout'}
<Timeout />
{:case 'error'}
<ErrorMessage />
{:default}
<div>{status}</div>
{/switch}
<!-- or -->
{#case type}
{:when '..'}
{:else}
{/case}
as opposed to:
{#if type === 'loading'}
<Loading />
{:elseif type === 'success'}
<Content />
{:elseif type === 'timeout'}
<Timeout />
{:elseif type === 'error'}
<ErrorMessage />
{:else}
<div>{status}</div>
{/switch}
<!-- or alternatively -->
<svelte:component this={(()=>{
switch (type) {
case 'loading':
return Loading;
case 'success':
return Content;
case 'timeout':
return Timeout;
case 'error':
return ErrorMessage;
}
})()}/>
EDIT: Just found this is a dupe: https://github.com/sveltejs/svelte/issues/530 - Feel free to close this and re-open the other issue.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Pack expansion: blocks, switch/case - Google Groups
Hello together, during development of my "Compile-time trie"-based string matcher, I wanted to use the compiler's advanced code generation for switch ...
Read more >c++ - Is There Anything Like a Templatized Case-Statement
The template version of a switch statement is a specialized template. template<size_t n> struct matching_type; template<> struct matching_type<sizeof(char)> ...
Read more >{% switch %} | Craft CMS Documentation | 2.x
This tag provides switch statement functionality to your templates. “Switch statements” offer a clean way to compare a variable against multiple possible ...
Read more >[SOLVED] Switch case in templates + using local shortcode ...
Is there a Go switch-case syntax that can be used in Hugo templates and shortcodes in place of multiple if-else conditions?
Read more >switch - Twig Tag | Branch CMS Documentation
The switch statement allows you to compare the value of a variable with multiple ... default %} block to handle values that don't...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Let us disagree with you on this. The same reasoning could be done in any language that offers a
switch
statement then! The statement exists for a reason, and helps people organize their code, write less cluttered code, etc. Isn’t this the idea behind Svelte? @Rich-HarrisClosing, but referring to this: https://github.com/sveltejs/svelte/issues/530#issuecomment-326818286
I also don’t think that having a switch statement really adds huge value, it simply increases the surface area, for no great benefit.
Sorry if this isn’t the answer you were looking for.
FWIW your example doesn’t help, do it thusly:
then change component via whatever means you like (switch, etc)