Text interpolation lets you show dynamic data inside your webpage easily. Mustache syntax {{ }} is a simple way to insert variables or expressions into your HTML.
Displaying user names or messages that change based on input
Showing live data like current time or counts
Updating parts of the page without reloading
Binding simple variables to text content in your UI
Creating templates that update automatically when data changes
Syntax
Vue
<div>{{ variableName }}</div>
Use double curly braces {{ }} to wrap the variable or expression you want to display.
Vue automatically updates the displayed text when the variable changes.
Examples
This example shows a simple greeting using a variable userName.
This Vue component uses mustache syntax to show a user name, today's date, and a lucky number. The displayed text updates automatically if the variables change.
Vue
<template>
<section>
<h1>Welcome, {{ user }}!</h1>
<p>Today is {{ newDate().toLocaleDateString() }}.</p>
<p>Your lucky number is {{ luckyNumber }}.</p>
</section>
</template>
<script setup>
import { ref } from'vue'const user = ref('Sam')
const luckyNumber = ref(7)
</script>
OutputSuccess
Important Notes
Mustache syntax only works inside the template section of Vue components.
It automatically escapes HTML to keep your app safe from code injection.
For more complex logic, use computed properties or methods instead of putting too much inside {{ }}.
Summary
Use {{ }} to insert variables or expressions into your HTML easily.
Vue updates the displayed text automatically when data changes.
Keep interpolation simple and use other Vue features for complex logic.
Practice
(1/5)
1. What does the mustache syntax {{ message }} do in a Vue template?
easy
A. It displays the value of the variable message inside the HTML.
B. It creates a new variable called message.
C. It comments out the text inside the braces.
D. It runs a JavaScript function named message.
Solution
Step 1: Understand mustache syntax purpose
The mustache syntax {{ }} is used in Vue to insert data values into HTML templates.
Step 2: Identify what {{ message }} does
It shows the current value of the variable named message in the rendered HTML.
Final Answer:
It displays the value of the variable message inside the HTML. -> Option A
Quick Check:
Mustache syntax = display variable value [OK]
Hint: Mustache {{ }} always shows variable content in HTML [OK]
Common Mistakes:
Thinking it creates variables
Assuming it runs functions
Confusing it with comments
2. Which of the following is the correct way to display a variable title inside a Vue template using mustache syntax?
easy
A. [[ title ]]
B. { title }
C. {{ title }}
D. {% title %}
Solution
Step 1: Recall Vue interpolation syntax
Vue uses double curly braces {{ }} to insert variables into templates.
Step 2: Match the correct syntax
Only {{ title }} matches Vue's mustache syntax for interpolation.
3. Given the Vue data: { name: 'Anna' }, what will be rendered by this template?
<div>Hello, {{ name }}!</div>
medium
A.
Hello, {{ name }}!
B.
Hello, Anna!
C.
Hello, name!
D.
Hello, !
Solution
Step 1: Identify the data value
The variable name has the value 'Anna'.
Step 2: Understand interpolation output
The mustache syntax replaces {{ name }} with 'Anna' in the rendered HTML.
Final Answer:
<div>Hello, Anna!</div> -> Option B
Quick Check:
Variable value replaces {{ name }} [OK]
Hint: Mustache {{ }} shows variable value, not the code itself [OK]
Common Mistakes:
Expecting to see {{ name }} literally
Confusing variable name with its value
Leaving interpolation empty
4. What is wrong with this Vue template snippet?
<div>{{ user.name </div>
medium
A. Mustache syntax cannot access object properties.
B. Variable user is not defined.
C. Interpolation must use square brackets instead of curly braces.
D. Missing closing curly braces for interpolation.
Solution
Step 1: Check mustache syntax completeness
The snippet has only one closing curly brace; mustache syntax requires double closing braces }}.
Step 2: Confirm correct syntax for interpolation
Proper interpolation should be {{ user.name }} with both opening and closing double braces.
Final Answer:
Missing closing curly braces for interpolation. -> Option D
Quick Check:
Mustache needs {{ and }} to work [OK]
Hint: Always close mustache with double braces }} [OK]
Common Mistakes:
Using single closing brace
Forgetting to close interpolation
Thinking square brackets are needed
5. You want to display a greeting that changes based on time using mustache syntax. Which template code correctly shows 'Good morning' if hour is less than 12, otherwise 'Good afternoon'?