Thuta Learning
Vue.js
BasicWeb DevelopmentBeginner

Template Syntax and Directives

Vue.jsLesson 420 min

What you'll walk away with

  • Use {{ }} interpolation
  • Bind attributes with v-bind
  • Read directive syntax comfortably

Explain it simply

Template Syntax and Directives, in plain language

In a template, {{ value }} is text interpolation. To bind an HTML attribute to a reactive value, use v-bind or its shorthand :. Never render user input directly with v-html.

Problem → Why → Solution

The problem — why this concept matters

Using {{ }} inside an attribute

A common mistake

Rendering untrusted user input with v-html

Core concept — understand how it works

Learn the basics of Vue template syntax and directives, including interpolation, v-bind, and v-html.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Use {{ }} interpolation

Core syntax or pattern

vue
<script setup>
const profile = {
  name: 'Su Su',
  avatar: '/avatar.png',
  active: true
}
</script>

<template>
  <h2>{{ profile.name }}</h2>
  <img :src="profile.avatar" :alt="profile.name">
  <p :class="{ online: profile.active }">Online</p>
</template>

Learn the basics of Vue template syntax and directives, including interpolation, v-bind, and v-html.

Read the code step by step

  1. props or event<script setup>

    In a template, {{ value }} is text interpolation. To bind an HTML attribute to a reactive value, use v-bind or its shorthand :. Never render user input directly with v-html.

  2. component and template</script>

    Learn the basics of Vue template syntax and directives, including interpolation, v-bind, and v-html.

  3. rendered Vue UI</template>

    Apply Template Syntax and Directives in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. rendered Vue UI

Success

Use {{ }} interpolation

Failure

Using {{ }} inside an attribute

Let's break it down simply

In a template, {{ value }} is text interpolation. To bind an HTML attribute to a reactive value, use v-bind or its shorthand :. Never render user input directly with v-html.

vue
<script setup>
const profile = {
  name: 'Su Su',
  avatar: '/avatar.png',
  active: true
}
</script>

<template>
  <h2>{{ profile.name }}</h2>
  <img :src="profile.avatar" :alt="profile.name">
  <p :class="{ online: profile.active }">Online</p>
</template>
You should see
Su Su
[avatar image]
Online

Try it yourself

Create a product object and display its title, price, and image src in the template.

Template Syntax — Vue.js

Real-world usage

How to apply it in a real project

Apply Template Syntax and Directives in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Create a product object and display its title, price, and image src in the template.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
vue
<script setup>
const profile = {
  name: 'Su Su',
  avatar: '/avatar.png',
  active: true
}
</script>

<template>
  <h2>{{ profile.name }}</h2>
  <img :src="profile.avatar" :alt="profile.name">
  <p :class="{ online: profile.active }">Online</p>
</template>

Expected: Use {{ }} interpolation

Show hint

Apply Template Syntax and Directives in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Template Syntax and Directives?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • props or event → component and template → rendered Vue UI
  • Learn the basics of Vue template syntax and directives, including interpolation, v-bind, and v-html.
  • Using {{ }} inside an attribute
  • Apply Template Syntax and Directives in a small example and test normal, edge, and failure cases.

Template Syntax — Vue.js

Easy traps

  • Using {{ }} inside an attribute
  • Rendering untrusted user input with v-html

Exercise

Create a product object and display its title, price, and image src in the template.

You'll know it worked when: Su Su [avatar image] Online

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Template Syntax and Directives | Thuta Learning