Template Syntax & Interpolation

A Vue template is HTML with a small amount of extra syntax layered on top — double curly braces to print a value, and a handful of special attributes to bind data to the rest of the page.

Text interpolation with {{ }}

Anything inside double curly braces is a JavaScript expression, evaluated against the component's data and re-evaluated automatically whenever that data changes:

HTML template
<p>Hello, {{ name }}! You have {{ items.length }} items.</p>
Rendered, with name: 'Priya' and items: ['a','b','c']
Hello, Priya! You have 3 items.

Anything that's valid as a single JavaScript expression works inside the braces — property access, simple arithmetic, ternaries — but not full statements like if or a for loop. Those have their own dedicated syntax, covered in the next lesson.

Binding attributes with v-bind (or the : shorthand)

Curly braces only work for text content — to bind data into an HTML attribute, you need v-bind, almost always written with its shorthand, a leading colon:

HTML template
<img :src="avatarUrl" :alt="userName">
<a :href="profileLink">View profile</a>
Rendered, with avatarUrl: '/img/priya.png'
<img src="/img/priya.png" alt="Priya">
<a href="/users/priya">View profile</a>

:src="avatarUrl" means "set the src attribute to whatever avatarUrl currently holds" — and like text interpolation, it updates automatically if avatarUrl changes later.

Binding classes conditionally

A common pattern is toggling a CSS class based on a boolean, which :class supports directly with an object — each key is a class name, each value decides whether it's applied:

HTML template
<p :class="{ error: hasError, bold: isImportant }">Status message</p>
Rendered, with hasError: true, isImportant: false
<p class="error">Status message</p>
Note: {{ }} only ever appears in text content — never inside an HTML tag's attributes. Writing <img src="{{ avatarUrl }}"> is a common beginner mistake; it won't work, and :src="avatarUrl" is the correct form for binding into an attribute.