question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Switch / Case blocks for templates

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
mossaibycommented, May 3, 2021

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-Harris

3reactions
antonycommented, Jul 4, 2020

Closing, 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:

import Blah from 'Blah.svelte'
let component = Blah

<svelte:component this={component} />

then change component via whatever means you like (switch, etc)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found