Svelte Template Basics

The template is the markup (HTML) of a .svelte file. In the template, {expression} puts a JavaScript value on the page: {name}, {price * 2}…

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

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>

{@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 &lt;, 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

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.

What you will learn