Skip to content

Commit 1ea5d8a

Browse files
authored
Tutorial part two (#453)
* note * snippet placeholder * add snippet exercise
1 parent 6510d72 commit 1ea5d8a

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

β€Žapps/svelte.dev/content/tutorial/02-advanced-svelte/01-advanced-reactivity/02-reactive-classes/index.mdβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ class Box {
3131
// ...
3232
}
3333
```
34+
35+
> [!NOTE] In addition to `$state` and `$derived`, you can use `$state.raw` and `$derived.by` to define reactive fields.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<script>
2+
let emoji = 'πŸ™ˆ';
3+
let description = 'see no evil';
4+
</script>
5+
6+
<table>
7+
<thead>
8+
<tr>
9+
<th>emoji</th>
10+
<th>description</th>
11+
<th>unicode escape sequence</th>
12+
<th>html entity</th>
13+
</tr>
14+
</thead>
15+
16+
<tbody>
17+
<tr>
18+
<td>{emoji}</td>
19+
<td>{description}</td>
20+
<td>\u{emoji.charCodeAt(0).toString(16)}\u{emoji.charCodeAt(1).toString(16)}</td>
21+
<td>&amp#{emoji.codePointAt(0)}</td>
22+
</tr>
23+
</tbody>
24+
</table>
25+
26+
<style>
27+
th, td {
28+
padding: 0.5em;
29+
}
30+
31+
td:nth-child(3),
32+
td:nth-child(4) {
33+
font-family: monospace;
34+
}
35+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<table>
2+
<thead>
3+
<tr>
4+
<th>emoji</th>
5+
<th>description</th>
6+
<th>unicode escape sequence</th>
7+
<th>html entity</th>
8+
</tr>
9+
</thead>
10+
11+
<tbody>
12+
{#snippet monkey(emoji, description)}
13+
<tr>
14+
<td>{emoji}</td>
15+
<td>{description}</td>
16+
<td>\u{emoji.charCodeAt(0).toString(16)}\u{emoji.charCodeAt(1).toString(16)}</td>
17+
<td>&amp#{emoji.codePointAt(0)}</td>
18+
</tr>
19+
{/snippet}
20+
21+
{@render monkey('πŸ™ˆ', 'see no evil')}
22+
{@render monkey('πŸ™‰', 'hear no evil')}
23+
{@render monkey('πŸ™Š', 'speak no evil')}
24+
</tbody>
25+
</table>
26+
27+
<style>
28+
th, td {
29+
padding: 0.5em;
30+
}
31+
32+
td:nth-child(3),
33+
td:nth-child(4) {
34+
font-family: monospace;
35+
}
36+
</style>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: Snippets and render tags
3+
---
4+
5+
Snippets allow you to reuse content within a component, without extracting it out into a separate file.
6+
7+
In this exercise, we're creating a table of the [three wise monkeys](https://en.wikipedia.org/wiki/Three_wise_monkeys), along with their unicode escape sequences and HTML entities. So far, we have but a single monkey.
8+
9+
We could duplicate the markup, of course. Or we could create an array of `{ emoji, description }` objects and pass it into an `{#each ...}` block. But a neater solution is to encapsulate the markup in a reusable block.
10+
11+
Begin by _declaring a snippet_:
12+
13+
```svelte
14+
/// file: App.svelte
15+
+++{#snippet monkey()}+++
16+
<tr>
17+
<td>{emoji}</td>
18+
<td>see no evil</td>
19+
<td>\u{emoji.charCodeAt(0).toString(16)}\u{emoji.charCodeAt(1).toString(16)}</td>
20+
<td>&amp#{emoji.codePointAt(0)}</td>
21+
</tr>
22+
+++{/snippet}+++
23+
```
24+
25+
The monkey is no longer visible until we _render_ it. Let's do that:
26+
27+
```svelte
28+
/// file: App.svelte
29+
<tbody>
30+
{#snippet monkey()}...{/snippet}
31+
32+
+++{@render monkey()}+++
33+
</tbody>
34+
```
35+
36+
Before we can use the snippet for the rest of our monkeys, we need to pass data into the snippet. Snippets can have zero or more parameters:
37+
38+
```svelte
39+
/// file: App.svelte
40+
<tbody>
41+
+++{#snippet monkey(emoji, description)}...{/snippet}+++
42+
43+
+++{@render monkey('πŸ™ˆ', 'see no evil')}+++
44+
</tbody>
45+
```
46+
47+
Add the rest of the monkeys:
48+
49+
- `'πŸ™ˆ', 'see no evil'`
50+
- `'πŸ™‰', 'hear no evil'`
51+
- `'πŸ™Š', 'speak no evil'`
52+
53+
Finally, delete the `<script>` block we no longer need it:
54+
55+
```svelte
56+
/// file: App.svelte
57+
---<script>
58+
let emoji = 'πŸ™ˆ';
59+
let description = 'see no evil';
60+
</script>---
61+
```
62+
63+
> [!NOTE] Snippets can be declared anywhere in your component, but, like functions, are only visible to render tags in the same 'scope' or a child scope.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
title: Snippet parameters
3+
---
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Reusing content
3+
scope: { 'prefix': '/src/lib/', 'name': 'src' }
4+
focus: /src/lib/App.svelte
5+
---

0 commit comments

Comments
Β (0)