---
title: Template syntax
---
### Tags
---
A lowercase tag, like `
`, denotes a regular HTML element. A capitalised tag, such as `` or ``, indicates a *component*.
```sv
```
### Attributes and props
---
By default, attributes work exactly like their HTML counterparts.
```sv
```
---
As in HTML, values may be unquoted.
```sv
```
---
Attribute values can contain JavaScript expressions.
```sv
page {p}
```
---
Or they can *be* JavaScript expressions.
```sv
```
---
Boolean attributes are included on the element if their value is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) and excluded if it's [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy).
All other attributes are included unless their value is [nullish](https://developer.mozilla.org/en-US/docs/Glossary/Nullish) (`null` or `undefined`).
```html
This div has no title attribute
```
---
An expression might include characters that would cause syntax highlighting to fail in regular HTML, so quoting the value is permitted. The quotes do not affect how the value is parsed:
```sv
```
---
When the attribute name and value match (`name={name}`), they can be replaced with `{name}`.
```sv
```
---
By convention, values passed to components are referred to as *properties* or *props* rather than *attributes*, which are a feature of the DOM.
As with elements, `name={name}` can be replaced with the `{name}` shorthand.
```sv
```
---
*Spread attributes* allow many attributes or properties to be passed to an element or component at once.
An element or component can have multiple spread attributes, interspersed with regular ones.
```sv
```
---
*`$$props`* references all props that are passed to a component, including ones that are not declared with `export`. It is not generally recommended, as it is difficult for Svelte to optimise. But it can be useful in rare cases – for example, when you don't know at compile time what props might be passed to a component.
```sv
```
---
*`$$restProps`* contains only the props which are *not* declared with `export`. It can be used to pass down other unknown attributes to an element in a component. It shares the same optimisation problems as *`$$props`*, and is likewise not recommended.
```html
```
> The `value` attribute of an `input` element or its children `option` elements must not be set with spread attributes when using `bind:group` or `bind:checked`. Svelte needs to be able to see the element's `value` directly in the markup in these cases so that it can link it to the bound variable.
> Sometimes, the attribute order matters as Svelte sets attributes sequentially in JavaScript. For example, ``, Svelte will attempt to set the value to `1` (rounding up from 0.5 as the step by default is 1), and then set the step to `0.1`. To fix this, change it to ``.
> Another example is ``. Svelte will set the img `src` before making the img element `loading="lazy"`, which is probably too late. Change this to `` to make the image lazily loaded.
---
### Text expressions
```sv
{expression}
```
---
Text can also contain JavaScript expressions:
> If you're using a regular expression (`RegExp`) [literal notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor), you'll need to wrap it in parentheses.
```sv
Hello {name}!
{a} + {b} = {a + b}.
{(/^[A-Za-z ]+$/).test(value) ? x : y}
```
### Comments
---
You can use HTML comments inside components.
```sv
Hello world
```
---
Comments beginning with `svelte-ignore` disable warnings for the next block of markup. Usually, these are accessibility warnings; make sure that you're disabling them for a good reason.
```sv
```
### {#if ...}
```sv
{#if expression}...{/if}
```
```sv
{#if expression}...{:else if expression}...{/if}
```
```sv
{#if expression}...{:else}...{/if}
```
---
Content that is conditionally rendered can be wrapped in an if block.
```sv
{#if answer === 42}
what was the question?
{/if}
```
---
Additional conditions can be added with `{:else if expression}`, optionally ending in an `{:else}` clause.
```sv
{#if porridge.temperature > 100}
too hot!
{:else if 80 > porridge.temperature}
too cold!
{:else}
just right!
{/if}
```
(Blocks don't have to wrap elements, they can also wrap text within elements!)
### {#each ...}
```sv
{#each expression as name}...{/each}
```
```sv
{#each expression as name, index}...{/each}
```
```sv
{#each expression as name (key)}...{/each}
```
```sv
{#each expression as name, index (key)}...{/each}
```
```sv
{#each expression as name}...{:else}...{/each}
```
---
Iterating over lists of values can be done with an each block.
```sv
Shopping list
{#each items as item}
{item.name} x {item.qty}
{/each}
```
You can use each blocks to iterate over any array or array-like value — that is, any object with a `length` property.
---
An each block can also specify an *index*, equivalent to the second argument in an `array.map(...)` callback:
```sv
{#each items as item, i}
{i + 1}: {item.name} x {item.qty}
{/each}
```
---
If a *key* expression is provided — which must uniquely identify each list item — Svelte will use it to diff the list when data changes, rather than adding or removing items at the end. The key can be any object, but strings and numbers are recommended since they allow identity to persist when the objects themselves change.
```sv
{#each items as item (item.id)}
{item.name} x {item.qty}
{/each}
{#each items as item, i (item.id)}
{i + 1}: {item.name} x {item.qty}
{/each}
```
---
You can freely use destructuring and rest patterns in each blocks.
```sv
{#each items as { id, name, qty }, i (id)}
{i + 1}: {name} x {qty}
{/each}
{#each objects as { id, ...rest }}
{id}
{/each}
{#each items as [id, ...rest]}
{id}
{/each}
```
---
An each block can also have an `{:else}` clause, which is rendered if the list is empty.
```sv
{#each todos as todo}
{todo.text}
{:else}
No tasks today!
{/each}
```
### {#await ...}
```sv
{#await expression}...{:then name}...{:catch name}...{/await}
```
```sv
{#await expression}...{:then name}...{/await}
```
```sv
{#await expression then name}...{/await}
```
```sv
{#await expression catch name}...{/await}
```
---
Await blocks allow you to branch on the three possible states of a Promise — pending, fulfilled or rejected. In SSR mode, only the pending state will be rendered on the server.
```sv
{#await promise}
waiting for the promise to resolve...
{:then value}
The value is {value}
{:catch error}
Something went wrong: {error.message}
{/await}
```
---
The `catch` block can be omitted if you don't need to render anything when the promise rejects (or no error is possible).
```sv
{#await promise}
waiting for the promise to resolve...
{:then value}
The value is {value}
{/await}
```
---
If you don't care about the pending state, you can also omit the initial block.
```sv
{#await promise then value}
The value is {value}
{/await}
```
---
Similarly, if you only want to show the error state, you can omit the `then` block.
```sv
{#await promise catch error}
The error is {error}
{/await}
```
### {#key ...}
```sv
{#key expression}...{/key}
```
Key blocks destroy and recreate their contents when the value of an expression changes.
---
This is useful if you want an element to play its transition whenever a value changes.
```sv
{#key value}
{value}
{/key}
```
---
When used around components, this will cause them to be reinstantiated and reinitialised.
```sv
{#key value}
{/key}
```
### {@html ...}
```sv
{@html expression}
```
---
In a text expression, characters like `<` and `>` are escaped; however, with HTML expressions, they're not.
The expression should be valid standalone HTML — `{@html "
"}content{@html "
"}` will *not* work, because `
` is not valid HTML. It also will *not* compile Svelte code.
> 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.
```sv
{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.
```sv
{@debug user}
Hello {user.firstname}!
```
---
`{@debug ...}` accepts a comma-separated list of variable names (not arbitrary expressions).
```sv
{@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.
### {@const ...}
```sv
{@const assignment}
```
---
The `{@const ...}` tag defines a local constant.
```sv
{#each boxes as box}
{@const area = box.width * box.height}
{box.width} * {box.height} = {area}
{/each}
```
`{@const}` is only allowed as direct child of `{#if}`, `{:else if}`, `{:else}`, `{#each}`, `{:then}`, `{:catch}`, `` or ``.
### Element directives
As well as attributes, elements can have *directives*, which control the element's behaviour in some way.
#### on:*eventname*
```sv
on:eventname={handler}
```
```sv
on:eventname|modifiers={handler}
```
---
Use the `on:` directive to listen to DOM events.
```sv
```
---
Handlers can be declared inline with no performance penalty. As with attributes, directive values may be quoted for the sake of syntax highlighters.
```sv
```
---
Add *modifiers* to DOM events with the `|` character.
```sv
```
The following modifiers are available:
* `preventDefault` — calls `event.preventDefault()` before running the handler
* `stopPropagation` — calls `event.stopPropagation()`, preventing the event reaching the next element
* `passive` — improves scrolling performance on touch/wheel events (Svelte will add it automatically where it's safe to do so)
* `nonpassive` — explicitly set `passive: false`
* `capture` — fires the handler during the *capture* phase instead of the *bubbling* phase
* `once` — remove the handler after the first time it runs
* `self` — only trigger handler if `event.target` is the element itself
* `trusted` — only trigger handler if `event.isTrusted` is `true`. I.e. if the event is triggered by a user action.
Modifiers can be chained together, e.g. `on:click|once|capture={...}`.
---
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.
```sv
```
---
It's possible to have multiple event listeners for the same event:
```sv
```
#### bind:*property*
```sv
bind:property={variable}
```
---
Data ordinarily flows down, from parent to child. The `bind:` directive allows data to flow the other way, from child to parent. Most bindings are specific to particular elements.
The simplest bindings reflect the value of a property, such as `input.value`.
```sv
```
---
If the name matches the value, you can use shorthand.
```sv
```
---
Numeric input values are coerced; even though `input.value` is a string as far as the DOM is concerned, Svelte will treat it as a number. If the input is empty or invalid (in the case of `type="number"`), the value is `undefined`.
```sv
```
---
On `` elements with `type="file"`, you can use `bind:files` to get the [`FileList` of selected files](https://developer.mozilla.org/en-US/docs/Web/API/FileList). It is readonly.
```sv
```
---
If you're using `bind:` directives together with `on:` directives, the order that they're defined in affects the value of the bound variable when the event handler is called.
```sv
```
Here we were binding to the value of a text input, which uses the `input` event. Bindings on other elements may use different events such as `change`.
##### Binding `