--- 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: ```htmlYou 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: ```htmlThis 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 `{:elseif ...}`: ```html {#if x > 10}{x} is greater than 10
{:elseif 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](guide#event-handlers), [bindings](guide#bindings), [transitions](guide#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: ```htmlCount: {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}