some more content

pull/2143/head
Richard Harris 7 years ago
parent 41793b6694
commit 1ac5b51f39

@ -2,5 +2,14 @@
title: Else-if blocks
---
Just like in JavaScript, we can 'chain' related conditional blocks together:
Multiple conditions can be 'chained' together with `else if`:
```html
{#if x > 10}
<p>{x} is greater than 10</p>
{:else if 5 > x}
<p>{x} is less than 5</p>
{:else}
<p>{x} is between 5 and 10</p>
{/if}
```

@ -0,0 +1,15 @@
<script>
let cats = [
{ id: 'J---aiyznGQ', name: 'Keyboard Cat' },
{ id: 'z_AbfPXTKms', name: 'Maru' },
{ id: 'OUtn3pvWmpg', name: 'Henri The Existential Cat' }
];
</script>
<h1>The Famous Cats of YouTube</h1>
<ul>
<!-- open each block -->
<li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">{cat.name}</a></li>
<!-- close each block -->
</ul>

@ -0,0 +1,15 @@
<script>
let cats = [
{ id: 'J---aiyznGQ', name: 'Keyboard Cat' },
{ id: 'z_AbfPXTKms', name: 'Maru' },
{ id: 'OUtn3pvWmpg', name: 'Henri The Existential Cat' }
];
</script>
<h1>The Famous Cats of YouTube</h1>
<ul>
{#each cats as { id, name }}
<li><a target="_blank" href="https://www.youtube.com/watch?v={id}">{name}</a></li>
{/each}
</ul>

@ -0,0 +1,17 @@
---
title: Each blocks
---
If you need to loop over lists of data, use an `each` block:
```html
<ul>
{#each cats as cat}
<li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">{cat.name}</a></li>
{/each}
</ul>
```
> The expression (`cats`, in this case) can be any array or array-like object (i.e. it has a `length` property). You can loop over generic iterables with `each [...iterable]`.
If you prefer, you can use destructuring — `each cats as { id, name }` — and replace `cat.id` and `cat.name` with `id` and `name`.

@ -38,7 +38,7 @@ Side-quest: create a 'Svelte for new developers' blog post that assumes no knowl
## Logic
* [x] If blocks
* [ ] Else/elseif blocks
* [x] Else/elseif blocks
* [ ] Each blocks
* [ ] Keyed each blocks (maybe? kind of need to cover transitions before we can make this obvious)
* [ ] Await blocks

Loading…
Cancel
Save