Include examples of if/else around text nodes

Where if/else isn't the lesson being taught, in some cases show how it
can be wrapped around text nodes. This is to teach users about Svelte's
template compilation whilst not obfuscating the lessons.
pull/4032/head
Henry Blyth 6 years ago
parent 13ef75be22
commit 2517e537e5

@ -167,15 +167,19 @@ Content that is conditionally rendered can be wrapped in an if block.
Additional conditions can be added with `{:else if expression}`, optionally ending in an `{:else}` clause. Additional conditions can be added with `{:else if expression}`, optionally ending in an `{:else}` clause.
```html ```html
{#if porridge.temperature > 100} <p>
<p>too hot!</p> {#if porridge.temperature > 100}
{:else if 80 > porridge.temperature} too hot!
<p>too cold!</p> {:else if 80 > porridge.temperature}
{:else} too cold!
<p>just right!</p> {:else}
{/if} just right!
{/if}
</p>
``` ```
(Blocks don't have to wrap elements, they can also wrap text within elements!)
### {#each ...} ### {#each ...}

@ -16,4 +16,11 @@ Since the two conditions — `if user.loggedIn` and `if !user.loggedIn` — are
{/if} {/if}
``` ```
> A `#` character always indicates a *block opening* tag. A `/` character always indicates a *block closing* tag. A `:` character, as in `{:else}`, indicates a *block continuation* tag. Don't worry — you've already learned almost all the syntax Svelte adds to HTML. > A `#` character always indicates a *block opening* tag. A `/` character always indicates a *block closing* tag. A `:` character, as in `{:else}`, indicates a *block continuation* tag. Don't worry — you've already learned almost all the syntax Svelte adds to HTML.
Svelte is really good at surgically updating only the parts of the DOM that need changing, including text nodes. However, it does not optimise common code in if/else blocks like this, so the `<button>` element will be replaced whenever the condition is changed. To further improve this example, we can wrap just the text:
```html
<button on:click="{toggle}">
Log {#if user.loggedIn}out{:else}in{/if}
</button>
```

@ -7,12 +7,14 @@
Yes! Send me regular email spam Yes! Send me regular email spam
</label> </label>
{#if yes} <p>
<p>Thank you. We will bombard your inbox and sell your personal details.</p> {#if yes}
{:else} Thank you. We will bombard your inbox and sell your personal details.
<p>You must opt in to continue. If you're not paying, you're the product.</p> {:else}
{/if} You must opt in to continue. If you're not paying, you're the product.
{/if}
</p>
<button disabled={!yes}> <button disabled={!yes}>
Subscribe Subscribe
</button> </button>

@ -7,12 +7,14 @@
Yes! Send me regular email spam Yes! Send me regular email spam
</label> </label>
{#if yes} <p>
<p>Thank you. We will bombard your inbox and sell your personal details.</p> {#if yes}
{:else} Thank you. We will bombard your inbox and sell your personal details.
<p>You must opt in to continue. If you're not paying, you're the product.</p> {:else}
{/if} You must opt in to continue. If you're not paying, you're the product.
{/if}
</p>
<button disabled={!yes}> <button disabled={!yes}>
Subscribe Subscribe
</button> </button>

@ -42,13 +42,13 @@
Raspberry ripple Raspberry ripple
</label> </label>
{#if flavours.length === 0} <p>
<p>Please select at least one flavour</p> {#if flavours.length === 0}
{:else if flavours.length > scoops} Please select at least one flavour
<p>Can't order more flavours than scoops!</p> {:else if flavours.length > scoops}
{:else} Can't order more flavours than scoops!
<p> {:else}
You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'} You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'}
of {join(flavours)} of {join(flavours)}
</p> {/if}
{/if} </p>

@ -40,13 +40,13 @@
</label> </label>
{/each} {/each}
{#if flavours.length === 0} <p>
<p>Please select at least one flavour</p> {#if flavours.length === 0}
{:else if flavours.length > scoops} Please select at least one flavour
<p>Can't order more flavours than scoops!</p> {:else if flavours.length > scoops}
{:else} Can't order more flavours than scoops!
<p> {:else}
You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'} You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'}
of {join(flavours)} of {join(flavours)}
</p> {/if}
{/if} </p>

@ -40,13 +40,13 @@
</label> </label>
{/each} {/each}
{#if flavours.length === 0} <p>
<p>Please select at least one flavour</p> {#if flavours.length === 0}
{:else if flavours.length > scoops} Please select at least one flavour
<p>Can't order more flavours than scoops!</p> {:else if flavours.length > scoops}
{:else} Can't order more flavours than scoops!
<p> {:else}
You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'} You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'}
of {join(flavours)} of {join(flavours)}
</p> {/if}
{/if} </p>

@ -41,13 +41,13 @@
{/each} {/each}
</select> </select>
{#if flavours.length === 0} <p>
<p>Please select at least one flavour</p> {#if flavours.length === 0}
{:else if flavours.length > scoops} Please select at least one flavour
<p>Can't order more flavours than scoops!</p> {:else if flavours.length > scoops}
{:else} Can't order more flavours than scoops!
<p> {:else}
You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'} You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'}
of {join(flavours)} of {join(flavours)}
</p> {/if}
{/if} </p>

Loading…
Cancel
Save