Vue Tutorial menu

Vue Template Syntax

Vue templates are HTML with extra features that connect the page to your data.

Text Interpolation

Use double curly braces, often called mustache syntax, to show a value:

Example

vue
<script setup>
const name = "Bilal";
const fee = 4500;
</script>

<template>
  <p>Student: {{ name }}</p>
  <p>Monthly fee: Rs {{ fee }}</p>
</template>
Output
Student: Bilal
Monthly fee: Rs 4500

Every variable and function declared in <script setup> is available in the template automatically.

JavaScript Expressions

Inside {{ }} you can write any single JavaScript expression:

Example

vue
<script setup>
const price = 1200;
const quantity = 3;
const city = "karachi";
const isMember = true;
</script>

<template>
  <p>Total: Rs {{ price * quantity }}</p>
  <p>City: {{ city.toUpperCase() }}</p>
  <p>{{ isMember ? "Member discount applied" : "No discount" }}</p>
</template>
Output
Total: Rs 3600
City: KARACHI
Member discount applied

Statements such as if, for or let x = 1 are not allowed inside {{ }}. Use directives (next lessons) or move the logic into the script.

Binding Attributes with v-bind

Curly braces do not work inside HTML attributes. To set an attribute from data, use the v-bind directive:

Example

vue
<script setup>
const logo = "/images/logo.png";
const shopUrl = "https://example.com/shop";
const isDisabled = true;
</script>

<template>
  <img v-bind:src="logo" alt="Shop logo" />
  <a v-bind:href="shopUrl">Visit shop</a>
  <button v-bind:disabled="isDisabled">Checkout</button>
</template>

Because v-bind is used so often, it has a short form: just a colon :.

Example

vue
<img :src="logo" alt="Shop logo" />
<a :href="shopUrl">Visit shop</a>

Both forms do exactly the same thing. Most Vue code uses the short form.

Binding Classes and Styles

:class accepts an object. Each class is added when its value is true:

Example

vue
<script setup>
const isActive = true;
const hasError = false;
</script>

<template>
  <div :class="{ active: isActive, error: hasError }">Status box</div>
</template>
Output
<div class="active">Status box</div>

:style accepts an object of CSS properties in camelCase:

Example

vue
<p :style="{ color: 'green', fontSize: '20px' }">Payment received</p>

Raw HTML with v-html

{{ }} always shows text, so HTML tags appear as plain text. To render real HTML, use v-html:

Example

vue
<p v-html="'<strong>Bold notice</strong>'"></p>

Never use v-html with text typed by users. It can let attackers run scripts on your site (an XSS attack). Use it only for trusted content.

Summary

SyntaxMeaning
{{ value }}Show a value as text
v-bind:src="x"Set an attribute from data
:src="x"Short form of v-bind
:class="{ a: cond }"Add class a when cond is true
v-html="html"Render trusted HTML

Directives are special attributes that start with v-. You will meet v-if, v-for, v-on and v-model soon.

Exercise

Exercise

What is the short form of v-bind:href="link"?

Learn Vue with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp