In the first lesson, {{ price }} put a number inside a <span>, and the number changed when the data did. Those double braces are a small part of the template: a component's HTML, plus a little Vue syntax for putting data into it.
Think of the template as a printed receipt at Mrs. Sri's stall: "Menu: , Price: $". The cashier doesn't rewrite the receipt. They only fill in the blanks from the cashier's book. In Vue, the receipt is the template, the blanks are the double braces, the cashier's book is the data, and the cashier is Vue. When the data changes, what's filled in the blanks changes too.
Interpolation: {{ }} fills in text
This component has two pieces of data, menu and price, and shows both in its template:
const menu = ref("Iced Tea")
const price = ref(5)
---template---
<div>
<h2>{{ menu }}</h2>
<p>Price: ${{ price }}</p>
</div>
The HTML that Vue puts on the page:
<div>
<h2>Iced Tea</h2>
<p>Price: $5</p>
</div>
- Putting a data value into text with
{{ menu }}is called interpolation. The double braces are also called the mustache syntax, because they look like a moustache. Vue replaces{{ menu }}with the value ofmenu. - The text around it,
Price: $, stays written in the template. Only the part that can change comes from the data. - The exercise uses the same pattern:
<h1>Hello, {{ name }}!</h1>withnameholding"Budi"showsHello, Budi!. The comma and the exclamation mark are written in the template, not in the data. - If
menu.valuebecomes"Chicken Noodles", the text in the<h2>changes with it. Text typed in directly, like<h2>Iced Tea</h2>, never changes.
Inside the braces: one JavaScript expression
What goes inside the double braces can be more than a variable name, as long as it's one expression: a piece of code that produces one value. This table uses the data menu = "Iced Tea", price = 5, count = 3 and stock = 0:
| In the template | What shows |
|---|---|
{{ menu.toUpperCase() }} |
ICED TEA |
{{ price * count }} |
15 |
{{ price.toFixed(2) }} |
5.00 |
{{ stock > 0 ? "In stock" : "Sold out" }} |
Sold out |
- String methods, like
toUpperCase(), can be called. Ifmessageholds"Hello Vue",{{ message.toUpperCase() }}showsHELLO VUE. toFixed(2)writes the number with two decimal places.- The ternary
condition ? a : bpicks one of two values:aif the condition is true,bif it is false. It's how you write "if" inside the braces. - Statements (commands that don't produce a value) are not allowed, for example
let,if, or two commands split by;. - For example,
{{ if (stock > 0) { "In stock" } }}. This template fails to compile (Vue can't turn it into working JavaScript code), so the whole component doesn't show. In development mode, the console writes[Vue warn]: Template compilation error: Error parsing JavaScript expression: avoid using JavaScript keyword as property name: "if". The Playground uses the production build, so all you get is a plain JavaScript error:Unexpected token 'if'. - A long calculation is tidier in the script. A later lesson, on computed, shows how.
Safe text: {{ }} and v-html
This code shows the same data, promo, in two ways:
const promo = ref("<b>10% off</b>")
---template---
<p>{{ promo }}</p>
<p v-html="promo"></p>
The result:
| Template | What shows |
|---|---|
<p>{{ promo }}</p> |
<b>10% off</b>, the tags show as text |
<p v-html="promo"></p> |
10% off, in bold |
{{ }}always puts its content in as text. HTML tags in the data don't become elements; they only show as written text. This protects the page from harmful HTML that other people type, such as in a customer's comment box.v-htmlis a directive, a special Vue attribute that starts withv-. This directive puts the value ofpromoin as real HTML, so the<b>makes the text bold.- Use
v-htmlonly for HTML you wrote yourself. Text that a user types and that goes throughv-htmlcan run harmful JavaScript code in other people's browsers. That attack is called XSS.
v-bind: data into attributes
This code fills two attributes from data:
const url = ref("/menu")
const soldOut = ref(true)
---template---
<a :href="url">See the full menu</a>
<button :disabled="soldOut">Order</button>
v-bind:href="url"binds thehrefattribute to theurldata:hreftakes its value fromurl, and changes whenurlchanges.v-bindis a directive too. Its shorthand is:href="url", as in this code, and it means exactly the same.- Since Vue 3.4, if the data has the same name as the attribute,
:hrefalone is enough. For example, with datahrefholding"/menu",<a :href>becomes<a href="/menu">. - What's inside the quotes after
:is a JavaScript expression, not text.:href="url"reads theurlvariable. - Some attributes have only two states, there or not there, like
disabled(the button can't be clicked). For these,:disabled="soldOut"adds the attribute whensoldOutistrue, and removes it when it'sfalse.
Before you look at the table below, take a guess: with <a href="{{ url }}">See the full menu</a>, where does the link go?
| What you write | The resulting href |
|---|---|
:href="url" |
/menu |
href="url" |
url, plain text |
href="{{ url }}" |
{{ url }}, exactly that text |
A link with href="{{ url }}" goes to an address that literally reads {{ url }}. Double braces only work between tags, not inside attributes. Vue 3 also gives no error or warning at all, so the link is quietly wrong.
⚠️ What quietly breaks a template
href="{{ url }}"doesn't read the data, because double braces don't work inside an attribute. Use:href="url".<h1>Hello, Budi!</h1>typed in directly looks the same as<h1>Hello, {{ name }}!</h1>, but doesn't change when the data changes. That's why, in the exercises, the checker also reads your code:nameandyearmust be read through{{ }}, andhrefthrough:href.if,letor;inside the braces make the template fail to compile, and the component disappears. Use an expression, such as a ternary.- Comments, names or messages from other people are shown with
{{ }}, notv-html, so any HTML in them shows as plain text.
TL;DR: A template is the component's HTML plus Vue's own syntax (way of writing). {{ menu }} (double braces) is called interpolation, or the mustache syntax. Vue puts the value of menu there as plain text: HTML tags in it show as written text. The text changes when the data changes. The inside of the double braces must be one expression (a piece of code that produces one value), such as {{ message.toUpperCase() }} or the ternary {{ stock > 0 ? "In stock" : "Sold out" }}, which picks one of two values. Commands like if or let are not allowed. The special attribute v-html puts data in as real HTML, so use it only for HTML of your own. Attributes are filled from data with v-bind:href="url" or its shorthand :href="url". href="{{ url }}" only makes a link to the text {{ url }}, because double braces don't work inside attributes.
⚡ Curly braces fill in text
Write:
const menu = ref("Iced Tea")
const price = ref(5)
---template---
<div>
<h2>{{ menu }}</h2>
<p>Price: ${{ price }}</p>
</div>
Meaning: The template (the part below the ---template--- line) is the component's HTML plus Vue syntax. {{ menu }} (double braces) is called interpolation: Vue replaces it with the value of the data menu. So the page shows Iced Tea and Price: $5. The text around it stays written in the template. Only the part that can change comes from the data.
In the exercise, the comma and the exclamation mark are written in the template too: <h1>Hello, {{ name }}!</h1>.
⚡ One expression inside
Write:
<p>{{ stock > 0 ? "In stock" : "Sold out" }}</p>
Guess first: only one expression, a piece of code that produces one value, is allowed inside the double braces. What happens if you write {{ if (stock > 0) { "In stock" } }}?
The template fails to compile (Vue can't turn it into working JavaScript code), and the whole component doesn't show. if is a statement, a command that doesn't produce a value, not an expression.
Meaning: The ternary condition ? a : b is how you write "if" inside the double braces. Its result is a if the condition is true, and b if it is false. So this code shows In stock if stock is more than 0, and Sold out if not.
⚡ Bind an attribute with :href
Write:
<a :href="url">See the full menu</a>
Guess first: url holds "/menu". Where does the link <a href="{{ url }}"> go?
To an address that literally reads {{ url }}, not /menu. Double braces don't work inside an attribute, and Vue 3 gives no error or warning at all.
Meaning: :href="url" is short for v-bind:href="url". v-bind is a directive (a special Vue attribute that starts with v-) that fills the href attribute from the variable url. So the link goes to /menu.