diff --git a/site/content/docs/02-template-syntax.md b/site/content/docs/02-template-syntax.md index aad7e3fc19..a6e17f8bc2 100644 --- a/site/content/docs/02-template-syntax.md +++ b/site/content/docs/02-template-syntax.md @@ -101,27 +101,7 @@ Text can also contain JavaScript expressions: ``` -### HTML expressions - -```sv -{@html expression} -``` - ---- - -In a text expression, characters like `<` and `>` are escaped. With HTML expressions, they're not. - -> Svelte does not sanitize expressions before injecting HTML. If the data comes from an untrusted source, you must sanitize it, or you are exposing your users to an XSS vulnerability. - -```html -
-

{post.title}

- {@html post.content} -
-``` - - -### If blocks +### {#if ...} ```sv {#if expression}...{/if} @@ -158,7 +138,7 @@ Additional conditions can be added with `{:else if expression}`, optionally endi ``` -### Each blocks +### {#each ...} ```sv {#each expression as name}...{/each} @@ -229,7 +209,7 @@ An each block can also have an `{:else}` clause, which is rendered if the list i ``` -### Await blocks +### {#await ...} ```sv {#await expression}...{:then name}...{:catch name}...{/await} @@ -283,7 +263,80 @@ If you don't care about the pending state, you can also omit the initial block. ``` -### DOM events +### {@html ...} + +```sv +{@html expression} +``` + +--- + +In a text expression, characters like `<` and `>` are escaped. With HTML expressions, they're not. + +> Svelte does not sanitize expressions before injecting HTML. If the data comes from an untrusted source, you must sanitize it, or you are exposing your users to an XSS vulnerability. + +```html +
+

{post.title}

+ {@html post.content} +
+``` + + +### {@debug ...} + +```sv +{@debug} +``` +```sv +{@debug var1, var2, ..., varN} +``` + +--- + +The `{@debug ...}` tag offers an alternative to `console.log(...)`. It logs the values of specific variables whenever they change, and pauses code execution if you have devtools open. + +It accepts a comma-separated list of variable names (not arbitrary expressions). + +```html + + +{@debug user} + +

Hello {user.firstname}!

+``` + +--- + +`{@debug ...}` accepts a comma-separated list of variable names (not arbitrary expressions). + +```html + +{@debug user} +{@debug user1, user2, user3} + + +{@debug user.firstname} +{@debug myArray[0]} +{@debug !isReady} +{@debug typeof user === 'object'} +``` + +The `{@debug}` tag without any arguments will insert a `debugger` statement that gets triggered when *any* state changes, as opposed to the specified variables. + + + +### Element directives + +As well as attributes, elements can have *directives*, which control the element's behaviour in some way. + + +#### [on:*eventname*](on_component_event) ```sv on:eventname={handler} @@ -324,6 +377,13 @@ Handlers can be declared inline with no performance penalty. As with attributes, Add *modifiers* to DOM events with the `|` character. +```html +
+ +
+``` + The following modifiers are available: * `preventDefault` — calls `event.preventDefault()` before running the handler @@ -334,13 +394,6 @@ The following modifiers are available: Modifiers can be chained together, e.g. `on:click|once|capture={...}`. -```html -
- -
-``` - --- If the `on:` directive is used without a value, the component will *forward* the event, meaning that a consumer of the component can listen for it. @@ -370,39 +423,11 @@ It's possible to have multiple event listeners for the same event: ``` -### Component events - -```sv -on:eventname={handler} -``` - ---- - -Components can emit events using [createEventDispatcher](docs#createEventDispatcher), or by forwarding DOM events. Listening for component events looks the same as listening for DOM events: - -```html - -``` - ---- - -As with DOM events, if the `on:` directive is used without a value, the component will *forward* the event, meaning that a consumer of the component can listen for it. - -```html - -``` - -### Element bindings +#### [bind:*property*](bind_element_property) ```sv bind:property={variable} ``` -```sv -bind:group={variable} -``` -```sv -bind:this={dom_node} -``` --- @@ -436,31 +461,8 @@ Numeric input values are coerced; even though `input.value` is a string as far a ``` -#### Binding related elements ---- - -Inputs that work together can use `bind:group`. - -```html - - - - - - - - - - - - -``` - -#### Binding `` value --- @@ -500,7 +502,7 @@ When the value of an `