In the first lesson, {stall} put the stall's name inside an <h1>. That single pair of curly braces is the heart of the template. The template is the markup of a .svelte file, meaning its HTML part: plain HTML, plus {} as the places where values from JavaScript come in.
The template is like the menu board at Mrs. Sri's stall. The words "Fried Rice" and "$" are painted on permanently. Next to them is an empty box, and every morning someone writes the day's price in it with chalk. The board isn't repainted when a price changes; only the box is filled in. In the template, the plain HTML is the painted words on the board, and {price} is that empty box, filled with the value of the variable price.
Curly braces: values from JavaScript
A variable you write in <script> can be used in the markup right away. Wrap its name in curly braces, and Svelte puts its value there:
<script>
let name = "Fried Rice"
let price = 15
</script>
<h2>{name}</h2>
<p>${price.toFixed(2)}</p>
<p>Two servings: ${(price * 2).toFixed(2)}</p>
<p>{name.toUpperCase()}</p>
The result on the page:
| Markup | Shows |
|---|---|
<h2>{name}</h2> |
Fried Rice |
${price.toFixed(2)} |
$15.00 |
Two servings: ${(price * 2).toFixed(2)} |
Two servings: $30.00 |
{name.toUpperCase()} |
FRIED RICE |
- Inside
{}any JavaScript expression is allowed. An expression is code that produces a value: a variable, a calculation, or a method call. toFixed(2)writes a number with two decimal places, the usual way to show a price:15becomes15.00. The$in front of the braces is plain text.- Statements such as
if,fororletare not allowed inside{}, because a statement is a command that produces no value. For branches and loops Svelte has the{#if}and{#each}blocks, covered later in Conditional Rendering in Svelte and List Rendering in Svelte. - One pair of braces is enough. If you write
{{name}}(Vue style), Svelte reads the inner pair of braces as an object, so the page shows[object Object].
In the second exercise, the pattern is the same, with a customer's name: let name = "Budi", then <h2>{name}</h2> shows Budi. The name is read from the variable, not typed again inside the <h2>.
Fixed text is written as it is
Curly braces are only needed for values that come from JavaScript. Text that never changes is written directly, like plain HTML:
<h1>Learning Svelte</h1>
<p>Svelte is a compiler.</p>
This code is also the answer to the first exercise: the <h1> holds Learning Svelte and the <p> holds Svelte is a compiler., with no <script> at all. If you like, <h1>Learning {topic}</h1> with let topic = "Svelte" gives exactly the same result.
Dynamic attributes
A dynamic attribute is an attribute whose value comes from a variable, as in these two images and one link:
<script>
let src = "/fried-rice.jpg"
let name = "Fried Rice"
let href = "https://wa.me/447700900123"
</script>
<img src={src} alt={name} />
<img {src} alt="Photo of {name}" />
<a {href}>Order via WhatsApp</a>
src={src}: thesrcattribute's value comes from the variablesrc. No quotes.{src}is shorthand forsrc={src}. The shorthand only works when the variable's name is exactly the attribute's name.alt="Photo of {name}": text and curly braces can be mixed inside quotes. The result isalt="Photo of Fried Rice".<a {href}>is the same as<a href={href}>: the "Order via WhatsApp" link points tohttps://wa.me/447700900123.- If you remove
alt, the compiler gives the warninga11y_missing_attribute:<img>element should have an alt attribute. Svelte helps you keep the page accessible, so that everyone can use it, including screen reader users.
{@html}: showing raw HTML
In this code, note holds raw HTML, meaning text that contains HTML tags. Its content is shown in two ways:
<script>
let note = "<b>Spicy</b> level 3"
</script>
<p>{note}</p>
<p>{@html note}</p>
| Markup | Shows |
|---|---|
<p>{note}</p> |
<b>Spicy</b> level 3, with the <b> tag shown as text |
<p>{@html note}</p> |
Spicy level 3, with "Spicy" in bold |
{note} is always treated as text. Before putting it in, Svelte turns < into <, the HTML code that shows the < sign as text. This step is called escaping. So the <b> tag shows as it is and never becomes an element. {@html note} is the opposite: its content goes in as real HTML.
Be careful: if what goes into {@html} comes from users, such as a note a buyer types, someone can send <img src=x onerror="...">. There is no image at the address x, so it fails to load, and then onerror runs that person's JavaScript on your page. That attack is called XSS. Only use {@html} for HTML you trust, such as your own content.
Svelte 4 and Svelte 5
All the syntax in this lesson is exactly the same in Svelte 5 and in Svelte 4, so older template tutorials are still correct. One small difference: when the value is null or undefined, Svelte 5 shows nothing, while Svelte 4 shows the word null or undefined.
⚠️ What often goes wrong with the braces
- Typing the value again.
<h2>Budi</h2>looks the same as<h2>{name}</h2>, but it doesn't change when the variable changes. The second exercise checks that the<h2>content starts with a curly brace. - Comments inside curly braces.
{/* ... */}(JSX style, from React) causes the compile errorUnexpected token. A compile error is an error the compiler gives while it translates your code. Comments in markup are written<!-- ... -->, like the TODO comment inside the<h2>of the second exercise's starter code. In that exercise, replace the TODO comment with{name}. - Shorthand with a different name.
let link = "https://wa.me/447700900123"then<a {link}>doesn't fillhref. The result is an attribute namedlink, and the text can't be clicked. In the same way<img {photo} />doesn't fillsrc. Writehref={link}orsrc={photo}. {@html}for text from users. Show plain text, including text from users, with{...}. This is safe because its content is escaped.
TL;DR: The template is the markup (HTML) of a .svelte file. In the template, {expression} puts a JavaScript value on the page: {name}, {price * 2}, {name.toUpperCase()}. An expression is code that produces a value. One pair of braces is enough. Text that doesn't change is written directly, without braces. An attribute takes its value from a variable with src={src}, and the shorthand {src} only works when the variable's name matches the attribute's name. {value} always shows as escaped text (any tags in it show as text), while {@html value} inserts real HTML. So never use {@html} for content from users: someone could put JavaScript into your page (an XSS attack). Comments in markup are written <!-- -->, not {/* */}.
⚡ Curly braces put a value in
Write:
<script>
let name = "Fried Rice"
let price = 15
</script>
<h2>{name}</h2>
<p>Two servings: ${(price * 2).toFixed(2)}</p>
Guess first: if you write {{name}} as in Vue, what shows?
[object Object]. Svelte reads the inner pair of braces as an object. One pair of curly braces is enough.
Meaning: A variable in <script> can be used in the markup (the HTML part of a .svelte file) right away. Inside {} any expression is allowed, meaning code that produces a value: a variable, a calculation, or a method call. Here toFixed(2) gives two decimal places ($30.00). Text that doesn't change, such as Two servings: $, is written directly, without braces.
⚡ Attributes can come from variables too
Write:
<img src={src} alt={name} />
<img {src} alt="Photo of {name}" />
Guess first: {src} is shorthand for src={src}. With let link = "https://wa.me/447700900123", then <a {link}>, can the text be clicked?
No. The result is an attribute named link, not href. The {src} shorthand only works when the variable's name is exactly the attribute's name.
Meaning: src and name here are variables in <script>. src={src} fills the src attribute with the value of the variable src, with no quotes. Text and curly braces can also be mixed inside quotes: with name holding "Fried Rice", alt="Photo of {name}" becomes alt="Photo of Fried Rice".
⚡ {@html} for raw HTML
Write:
<script>
let note = "<b>Spicy</b> level 3"
</script>
<p>{note}</p>
<p>{@html note}</p>
Meaning: note holds raw HTML, meaning text that contains tags. {note} always shows as text, so the <b> tag is visible as text. {@html note} inserts real HTML, so "Spicy" shows in bold. Never use {@html} for content from users, because it opens an XSS hole: someone else could run JavaScript on your page.