Template Basics — Vue

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…

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>

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

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

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>

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

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.

What you will learn