Template Fundamentals

Loading concept...

🎭 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?

  1. Vue looks at the word inside {{ }}
  2. Finds that word in your data
  3. Puts the value on the screen
  4. 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: &amp;lt;b&amp;gt;Hello&amp;lt;/b&amp;gt;"] --> B{Which Way?} B -->|Double Mustache| C["Shows: &amp;lt;b&amp;gt;Hello&amp;lt;/b&amp;gt;"] B -->|v-html| D["Shows: &lt;b&gt;Hello&lt;/b&gt;"]

⚠️ 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["&#123;&#123; data &#125;&#125;"] B1 --> B2["Auto-updates!"] C --> C1["v-html directive"] C1 --> C2["Renders actual HTML"] D --> D1["Math: &#123;&#123; a + b &#125;&#125;"] D --> D2["Methods: &#123;&#123; text.toUpperCase&amp;&#35;40;&amp;&#35;41; &#125;&#125;"] D --> D3["Ternary: &#123;&#123; x ? &&#35;39;yes&&#35;39; : &&#35;39;no&&#35;39; &#125;&#125;"]

🎮 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

  1. Templates = Your magic letter to the screen
  2. {{ mustaches }} = Show data as text
  3. v-html = Show actual HTML (use carefully!)
  4. 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! ✨

Loading story...

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Interactive content is coming soon.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Cheatsheet is coming soon.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Quiz is coming soon.

Flashcard Preview

Flashcard - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Stay Tuned!

Flashcards are coming soon.