diff --git a/site/content/docs/_old_docs/02-template-syntax.md b/site/content/docs/_old_docs/02-template-syntax.md deleted file mode 100644 index d915a8e37f..0000000000 --- a/site/content/docs/_old_docs/02-template-syntax.md +++ /dev/null @@ -1,281 +0,0 @@ ---- -title: Template syntax ---- - -Rather than reinventing the wheel, Svelte templates are built on foundations that have stood the test of time: HTML, CSS and JavaScript. There's very little extra stuff to learn. - - -### Tags - -Tags allow you to bind data to your template. Whenever your data changes (for example after `component.a = 3`), the DOM updates automatically. You can use any JavaScript expression in templates, and it will also automatically update: - -```html - -
{a} + {b} = {a + b}
-``` - -```json -/* { hidden: true } */ -{ - "a": 1, - "b": 2 -} -``` - -You can also use tags in attributes: - -```html - -You can hide this paragraph.
-``` - -```json -/* { hidden: true } */ -{ - color: "steelblue", - hideParagraph: false -} -``` -[Boolean attributes](https://www.w3.org/TR/html5/infrastructure.html#sec-boolean-attributes) like `hidden` will be omitted if the tag expression evaluates to false. Attributes will be removed from the element if their value is `undefined` or `null`. - -### HTML - -Ordinary tags render expressions as plain text. If you need your expression interpreted as HTML, wrap it in a special `@html` tag: - -```html - -This HTML: {content}
-Renders as: {@html content}
-``` - -```json -/* { hidden: true } */ -{ - content: "Some bold text." -} -``` - -As with regular tags, you can use any JavaScript expression in HTML tags, and it will automatically update the document when your data changes. - -> HTML is **not** sanitized before it is rendered! If you are displaying user input, you are responsible for first sanitizing it. Not doing so potentially opens you up to XSS attacks. - - -### If blocks - -Control whether or not part of your template is rendered by wrapping it in an if block. - -```html - -{#if user.loggedIn} - log out -{/if} - -{#if !user.loggedIn} - log in -{/if} -``` - -You can combine the two blocks above with `{:else}`: - -```html - -{#if user.loggedIn} - log out -{:else} - log in -{/if} -``` - -You can also use `{:else if ...}`: - -```html - -{#if x > 10} -{x} is greater than 10
-{:else if 5 > x} -{x} is less than 5
-{:else} -{x} is between 5 and 10
-{/if} -``` - -```json -/* { hidden: true } */ -{ - x: 7 -} -``` - -### Each blocks - -Iterate over lists of data: - -```html - -
- {x + 1},{y + 1}:
- {row[column]}
-
- {/each}
- wait for it...
-{:then answer} -the answer is {answer}!
-{:catch error} -well that's odd
-{/await} -``` - -If the expression in `{#await expression}` *isn't* a promise, Svelte skips ahead to the `then` section. - - -### Directives - -Directives allow you to add special instructions for adding [event handlers](docs#event-handlers), [bindings](docs#bindings), [transitions](docs#transitions) and so on. We'll cover each of those in later stages of this guide – for now, all you need to know is that directives can be identified by the `:` character: - -```html - -Count: {count}
- -``` - -```json -/* { hidden: true } */ -{ - count: 0 -} -``` - -> Technically, the `:` character is used to denote namespaced attributes in HTML. These will *not* be treated as directives, if encountered. - - -### Debug tags - -To inspect data as it changes and flows through your app, use a `{@debug ...}` tag: - -```html - - - -{@debug name} -this text is not bold
-this text is bold
-Count: {count}
- -``` - - -### External properties - -On the other hand, for the component to form part of a system, it needs to expose certain values so that they can be set from outside. These are called *props*, and we use the `export` keyword to differentiate them from internal state: - -```html - - - -Count: {count}
- -``` - -> Effectively, we're exporting a *contract* with the outside world. The `export` keyword normally means something different in JavaScript, so you might be surprised to see it used like this. Just roll with it for now! - -The `= 0` sets a default value for `count`, if none is provided. - -```js -const counter = new Counter({ - target: document.body, - props: { - count: 99 - } -}); - -counter.count; // 99 -counter.count += 1; // 100 -``` - -Props declared with `const` or `function` are *read-only* — they cannot be set from outside. This allows you to, for example, attach custom methods to your component: - -```js -component.doSomethingFun(); -``` - - -### Lifecycle hooks - -There are four 'hooks' provided by Svelte for adding control logic — `onMount`, `beforeUpdate`, `afterUpdate` and `onDestroy`. Import them directly from `svelte`: - -```html - - - - -``` - -> Lifecycle hooks do *not* run in server-side rendering (SSR) mode, with the exception of `onDestroy`. More on SSR later. diff --git a/site/content/docs/_old_docs/05-nested-components.md b/site/content/docs/_old_docs/05-nested-components.md deleted file mode 100644 index 1f5c34f8d5..0000000000 --- a/site/content/docs/_old_docs/05-nested-components.md +++ /dev/null @@ -1,147 +0,0 @@ ---- -title: Nested components ---- - -As well as containing elements (and `if` blocks and `each` blocks), Svelte components can contain *other* Svelte components. - -```html - - - - -``` - -```html - -I am a nested component. The answer is {answer}
-``` - -That's similar to doing this... - -```js -import Widget from './Widget.html'; - -const widget = new Widget({ - target: document.querySelector('.widget-container'), - props: { - answer: 42 - } -}); -``` - -...except that Svelte takes care of destroying the child component when the parent is destroyed, and keeps props in sync if they change. - -> Component names must be capitalised, following the widely-used JavaScript convention of capitalising constructor names. It's also an easy way to distinguish components from elements in your template. - - -### Props - -Props, short for 'properties', are the means by which you pass data down from a parent to a child component — in other words, they're just like attributes on an element. As with element attributes, prop values can contain any valid JavaScript expression. - -Often, the name of the property will be the same as the value, in which case we can use a shorthand: - -```html - - -This is a box. It can contain anything.
-the box is empty!
-{countdown}
-liftoff!
-{/if} -``` - -```json -/* { hidden: true } */ -{ - countdown: 5 -} -``` - - -### `Red {name}
-``` - -```html - -Blue {name}
-``` - -The expression inside the `this="{...}"` can be any valid JavaScript expression. - - -### `{key === ' ' ? 'Space' : key} (code {keyCode})
-{:else} -click in this window and press any key
-{/if} -``` - -You can also bind to certain values — so far `innerWidth`, `outerWidth`, `innerHeight`, `outerHeight`, `scrollX`, `scrollY` and `online`: - -```html - -user has scrolled {y} pixels
-``` - - -### `Count: {count}
- -``` - -```json -/* { hidden: true } */ -{ - count: 0 -} -``` - -For more complicated behaviours, you'll probably want to declare an event handler in your ` - -Count: {count}
- -``` - -```json -/* { hidden: true } */ -{ - count: 0 -} -``` - - -### Event handler modifiers - -While you can invoke methods like `event.stopPropagation` directly... - -```html - -Select a category:
- -{#each categories as category} - -{/each} - - -``` - -When the user clicks a button, the component will fire a `select` event, where the `event` object has a `category` property. Any component that nests `Select a category:
- -{#each categories as category} - -{/each} - - -``` - -Just as `this` in an element's event handler refers to the element itself, in a component event handler `this` refers to the component firing the event. - -There is also a shorthand for listening for and re-firing an event unchanged. - -```html - - -