📝 Vue Template Syntax – Complete Guide with Examples (2025)
🧲 Introduction – What Is Vue Template Syntax?
Vue.js templates are at the heart of how we build UIs with Vue. The syntax blends familiar HTML with enhanced capabilities like interpolation, directives, and binding, making it easy to declaratively render dynamic content.
🎯 In this guide, you’ll learn:
- How Vue templates extend regular HTML
- Different interpolation methods and directives
- Syntax rules for dynamic binding, conditional rendering, loops, and events
- Best practices with real examples
📐 What Are Vue Templates?
Vue templates are HTML-like syntax used to describe your component’s structure. Vue compiles these templates into optimized JavaScript render functions during the build phase.
A valid Vue template can be:
- A string in the
templateoption - An inline DOM element using
el - Or more commonly, part of a Single File Component (SFC) inside
<template></template>tags
🧩 Basic Interpolation – Mustache Syntax
The most common way to bind data in Vue templates is using double curly braces {{ }}, known as Mustache syntax.
✅ Example:
<template>
<p>Hello, {{ name }}!</p>
</template>
<script>
export default {
data() {
return {
name: 'Flavio'
};
}
}
</script>
🧠 Explanation:
{{ name }}dynamically inserts the value ofnameinto the DOM.- Any change to
nameautomatically updates the rendered output.
🧠 JavaScript Expressions in Interpolation
You can embed JavaScript expressions inside {{ }}:
<p>{{ name === 'Flavio' ? 'Hi Flavio!' : 'Hi there!' }}</p>
<p>{{ Math.sqrt(16) }}</p>
📌 Note: Only a single expression is allowed; no control flow or complex logic.
🔗 Attribute Binding – v-bind
To bind data to HTML attributes, use the v-bind directive:
<img v-bind:src="imageUrl" />
Or use the shorthand:
<img :src="imageUrl" />
🧠 Use Case:
- Bind
href,class,id,style, or any attribute based on your component’s data.
🔁 Conditional Rendering – v-if, v-else, v-show
Control element visibility using directives:
<p v-if="isLoggedIn">Welcome back!</p>
<p v-else>Login to continue</p>
🔹 v-if adds/removes elements from the DOM.
🔹 v-show toggles the display CSS style (element always exists).
🔃 List Rendering – v-for
Use v-for to loop over arrays or objects:
<ul>
<li v-for="(fruit, index) in fruits" :key="index">{{ fruit }}</li>
</ul>
🧠 Behind the Scenes:
- Vue tracks changes efficiently by using the
keyattribute for diffing.
🎛️ Event Handling – v-on
Attach event listeners with v-on or the shorthand @:
<button v-on:click="handleClick">Click me</button>
<!-- Shorthand -->
<button @click="handleClick">Click me</button>
Pass parameters and access the event object:
<button @click="handleClick('Hello', $event)">Click me</button>
🔄 Two-Way Binding – v-model
For form elements, Vue supports two-way data binding with v-model:
<input v-model="message" placeholder="Type something..." />
<p>{{ message }}</p>
🧠 Works with: <input>, <textarea>, <select> and custom components.
🔠 v-text and v-html
v-textreplaces content (alternative to interpolation)v-htmlrenders raw HTML (use cautiously to avoid XSS)
<span v-text="title"></span>
<span v-html="htmlContent"></span>
🧷 v-once – Render Static Content
To prevent updates and re-renders:
<p v-once>{{ staticText }}</p>
Useful for performance when rendering unchanging data.
⚙️ Using Computed Properties in Templates
Templates can reference computed properties to simplify logic:
computed: {
fullName() {
return this.firstName + ' ' + this.lastName;
}
}
<p>{{ fullName }}</p>
📌 Summary – Recap & Next Steps
Vue’s template syntax provides a clean, readable, and powerful way to build user interfaces. It feels like writing HTML—but with the superpowers of reactivity, logic, and component control.
🔍 Key Takeaways:
- Use
{{ }}for interpolation andv-bind,v-on,v-modelfor dynamic behavior - Templates support expressions but avoid complex logic inside them
- Use shorthand
:and@for cleaner code
⚙️ Real-World Relevance:
Mastering Vue’s template syntax accelerates your productivity in real projects—whether building forms, dashboards, or single-page apps.
❓ FAQ Section
❓ What is Vue template syntax?
✅ It’s an extended HTML syntax that allows dynamic rendering using interpolation, directives, and expressions.
❓ Can I use JavaScript inside {{ }}?
✅ Yes, you can use single expressions like {{ name + '!' }} or {{ Math.random() }}.
❓ What is the shorthand for v-bind and v-on?
✅ Use : for v-bind and @ for v-on.
Example: <a :href="link" @click="open()">Click</a>
❓ What is v-model used for?
✅ It creates two-way data binding between form inputs and your component’s data.
❓ What’s the difference between v-if and v-show?
✅ v-if adds/removes elements from the DOM. v-show toggles visibility using CSS.
Share Now :
