Template Syntax
Foundation
Tags
A lowercase tag, like <div>
, denotes a regular HTML element. A capitalised tag, such as <Widget>
or <Namespace.Widget>
, indicates a component.
Attributes and props
By default, attributes work exactly like their HTML counterparts.
As in HTML, values may be unquoted.
Attribute values can contain JavaScript expressions.
Or they can be JavaScript expressions.
Boolean attributes are included on the element if their value is truthy and excluded if it's falsy.
All other attributes are included unless their value is nullish (null
or undefined
).
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:
When the attribute name and value match (name={name}
), they can be replaced with {name}
.
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.
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.
$$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.
$$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.
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.
Text expressions
Text can also contain JavaScript expressions:
If you're using a regular expression (RegExp
) literal notation, you'll need to wrap it in parentheses.
Comments
You can use HTML comments inside components.
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.
{#if ...}
Content that is conditionally rendered can be wrapped in an if block.
Additional conditions can be added with {:else if expression}
, optionally ending in an {:else}
clause.
{#each ...}
Iterating over lists of values can be done with an each block.
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:
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.
You can freely use destructuring and rest patterns in each blocks.
An each block can also have an {:else}
clause, which is rendered if the list is empty.
{#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.
The catch
block can be omitted if you don't need to render anything when the promise rejects (or no error is possible).
If you don't care about the pending state, you can also omit the initial block.
Similarly, if you only want to show the error state, you can omit the then
block.
{#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.
When used around components, this will cause them to be reinstantiated and reinitialised.
{@html ...}
In a text expression, characters like <
and >
are escaped; however, with HTML expressions, they're not.
The expression should be valid standalone HTML — {@html "<div>"}content{@html "</div>"}
will not work, because </div>
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.
{@debug ...}
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.
{@debug ...}
accepts a comma-separated list of variable names (not arbitrary expressions).
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 ...}
The {@const ...}
tag defines a local constant.
{@const}
is only allowed as direct child of {#each}
, {:then}
, {:catch}
, <Component />
or <svelte:fragment />
.