🎭 Vue.js Template Fundamentals
The Magic Letter-Writing System
Imagine you have a magical letter. You write words on it, and whatever you write instantly appears on a billboard for everyone to see. That’s what Vue templates do — they’re your magic letter to the screen!
🌟 What Are Templates?
Think of a Vue template like a coloring book page. The page has shapes and spaces where you fill in colors. In Vue:
- The page = your HTML template
- The colors = your data
- The magic = Vue automatically updates the page when your colors change!
<template>
<div>
Hello, {{ name }}!
</div>
</template>
When name is “Alex”, the screen shows: Hello, Alex!
Change name to “Sam”? Screen instantly shows: Hello, Sam! ✨
📝 Template Syntax Basics
The Golden Rule
Vue templates look like normal HTML, but with superpowers!
graph TD A["Your HTML Template"] --> B{Vue Engine} B --> C["Magic Happens!"] C --> D["Screen Shows Your Data"]
Normal HTML vs Vue Template
| Normal HTML | Vue Template |
|---|---|
<p>Hello</p> |
<p>{{ greeting }}</p> |
| Text never changes | Text updates automatically! |
| Static billboard | Magic billboard |
🎯 Text Interpolation
The Double Mustache {{ }}
See those curly braces? They look like a mustache on its side! 🥸
<p>My name is {{ name }}</p>
<p>I am {{ age }} years old</p>
<p>{{ greeting }}, friend!</p>
What happens inside the mustache?
- Vue looks at the word inside
{{ }} - Finds that word in your data
- Puts the value on the screen
- Updates it whenever you change the data!
Real Example
// Your data (the "colors")
data() {
return {
petName: "Fluffy",
petType: "cat"
}
}
<!-- Your template (the "coloring book") -->
<p>I have a {{ petType }}.</p>
<p>Its name is {{ petName }}!</p>
Screen shows:
I have a cat. Its name is Fluffy!
🔮 Raw HTML Binding
When Text Isn’t Enough
Sometimes you want to show actual HTML, not just text. Like showing a red bold word!
The Problem:
{{ "<b>Bold</b>" }}
Shows literally: <b>Bold</b> 😕
The Solution: v-html
<span v-html="boldText"></span>
Shows: Bold 🎉
v-html is Like a Magic Portal
graph TD A["Data: &lt;b&gt;Hello&lt;/b&gt;"] --> B{Which Way?} B -->|Double Mustache| C["Shows: &lt;b&gt;Hello&lt;/b&gt;"] B -->|v-html| D["Shows: <b>Hello</b>"]
⚠️ Safety Warning!
v-html is powerful but only use it with data YOU control!
Think of it like this:
- Your friend gives you a note to read aloud ✅ Safe
- A stranger gives you a note to read aloud ❌ Maybe dangerous!
<!-- ✅ Safe: Your own data -->
<div v-html="myDescription"></div>
<!-- ❌ Risky: Data from users -->
<div v-html="userComment"></div>
🧮 Template Expressions
Your Template Can Do Math!
Inside {{ }}, you can write mini calculations!
<p>{{ 2 + 2 }}</p> <!-- Shows: 4 -->
<p>{{ price * 2 }}</p> <!-- Doubles the price -->
<p>{{ name.toUpperCase() }}</p> <!-- SHOUTS! -->
Simple Rules for Expressions
✅ These Work:
{{ age + 1 }} <!-- Math -->
{{ isHappy ? '😊' : '😢' }} <!-- Choices -->
{{ message.split('').reverse().join('') }}
{{ items.length }} <!-- Counting -->
❌ These Don’t Work:
{{ var x = 1 }} <!-- No creating variables -->
{{ if (ok) { } }} <!-- No if statements -->
{{ for (i in list) { } }} <!-- No loops -->
The One-Liner Rule
Think of it like a single sentence. You can say:
- “I have 5 apples” ✅
- “If I have apples then… else…” ❌ (too complicated!)
🎨 Putting It All Together
Here’s your Vue template superpower toolkit:
<template>
<div>
<!-- Text Interpolation -->
<h1>Welcome, {{ userName }}!</h1>
<!-- Expressions -->
<p>You have {{ coins * 100 }} points</p>
<p>Status: {{ isOnline ? 'Online' : 'Offline' }}</p>
<!-- Raw HTML -->
<div v-html="formattedBio"></div>
</div>
</template>
data() {
return {
userName: "Hero",
coins: 5,
isOnline: true,
formattedBio: "<em>A brave adventurer</em>"
}
}
Result on screen:
Welcome, Hero!
You have 500 points Status: Online A brave adventurer
🗺️ Quick Reference Map
graph TD A["Template Syntax"] --> B["Text Interpolation"] A --> C["Raw HTML"] A --> D["Expressions"] B --> B1["{{ data }}"] B1 --> B2["Auto-updates!"] C --> C1["v-html directive"] C1 --> C2["Renders actual HTML"] D --> D1["Math: {{ a + b }}"] D --> D2["Methods: {{ text.toUpperCase&#40;&#41; }}"] D --> D3["Ternary: {{ x ? &#39;yes&#39; : &#39;no&#39; }}"]
🎮 Remember These!
| Feature | Syntax | What It Does |
|---|---|---|
| Text Interpolation | {{ data }} |
Shows data as text |
| Raw HTML | v-html="data" |
Renders HTML code |
| Math Expression | {{ a + b }} |
Calculates values |
| Method Call | {{ str.toUpperCase() }} |
Transforms data |
| Conditional | {{ x ? 'A' : 'B' }} |
Picks between options |
🌈 The Magic Recap
- Templates = Your magic letter to the screen
- {{ mustaches }} = Show data as text
- v-html = Show actual HTML (use carefully!)
- Expressions = Do simple calculations right in your template
You did it! 🎉 You now understand how Vue templates talk to your screen. It’s like having a magic pen that writes whatever you think!
💡 One Last Thing
Vue templates are reactive. That fancy word means:
When your data changes, your screen updates automatically.
No extra work. No refreshing. Just magic! ✨
