Skip to content

Template Syntax

Vue templates use an HTML-based syntax that allows you to declaratively bind data to the rendered DOM.

  • Interpolations: {{ message }}
  • Directives: v-if, v-for, v-bind, v-on
  • Event handling: @click="doSomething"

Use double curly braces (mustache syntax) to display data:

<span>Message: {{ msg }}</span>

To output raw HTML, use the v-html directive:

<p v-html="rawHtml"></p>

Warning: Only use v-html with trusted content to avoid XSS vulnerabilities.

Use v-bind (or : shorthand) to bind attributes dynamically:

<div :id="dynamicId"></div>
<button :disabled="isDisabled">Button</button>

You can use JavaScript expressions in bindings:

<span>{{ number + 1 }}</span>
<span>{{ ok ? 'YES' : 'NO' }}</span>

Directives are special attributes with the v- prefix. Common ones include:

  • v-if, v-else, v-else-if for conditional rendering
  • v-for for list rendering
  • v-bind for binding attributes
  • v-on for event handling
  • : for v-bind
  • @ for v-on
<template>
<div>
<p>{{ message }}</p>
<button @click="increment">Add</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const message = ref('Hello!')
function increment() { /* ... */ }
</script>