Vue Templates

Vue Templates
They are written using HTML + Vue’s template syntax, allowing you to bind data, handle events, and render content dynamically.
What are Vue Templates?
A Vue template is an HTML-based syntax that tells Vue how to render the component.
- Uses standard HTML
- Enhanced with Vue directives
- Automatically updates when data changes
Template Syntax Overview
Text Interpolation ({{ }})
Used to display dynamic data.
- Also called Mustache syntax.
Raw HTML (v-html)
Renders HTML instead of plain text.
- Avoid untrusted content (XSS risk).
Vue Directives in Templates
Directives are special attributes starting with v-.
Commonly Used Directives
v-bind→ bind attributesv-on→ handle eventsv-if/v-show→ conditional renderingv-for→ list renderingv-model→ two-way binding
Attribute Binding
Event Handling in Templates
Conditional Rendering
List Rendering
Expressions in Templates
It support simple JavaScript expressions.
Allowed:
Not allowed:
Using <template> Tag
Used to group multiple elements without adding extra DOM nodes.
Template Sources
Templates can be written in multiple ways:
Inline Template (HTML)
Template String
Single File Component (Recommended)
Template vs Render Function
| Feature | Template | Render Function |
|---|---|---|
| Readability | Easy | Complex |
| HTML-like | Yes | No |
| Performance | Optimized | Optimized |
| Beginners | Best | Hard |
- Templates are preferred in most cases.
Best Practices
- Keep templates clean & readable
- Move logic to computed & methods
- Avoid complex expressions
- Use components to split UI
Common Mistakes
- Writing heavy logic inside templates
- Forgetting
keyinv-for Usingv-ifinsidev-forOverusingv-html
Summary Table
| Feature | Syntax |
|---|---|
| Interpolation | {{ value }} |
| Attribute bind | :src="url" |
| Event | @click="method" |
| Condition | v-if |
| Loop | v-for |
| Two-way bind | v-model |
Conclusion
Templates are the foundation of Vue UI development.
They combine HTML clarity with powerful reactivity, making UI creation simple and efficient.
