---
title: Template Expressions Must Be Single Expressions
impact: MEDIUM
impactDescription: Using statements instead of expressions in templates causes compilation errors
type: capability
tags: [vue3, template, expressions, interpolation, syntax]
---
# Template Expressions Must Be Single Expressions
**Impact: MEDIUM** - Vue templates only support single JavaScript expressions, not statements. Using variable declarations, if statements, or multiple statements will cause template compilation errors.
Template interpolation `{{ }}` and directive bindings evaluate JavaScript expressions that produce a value. Statements like `if`, `for`, variable declarations, or multi-line code blocks are not allowed.
## Task Checklist
- [ ] Use only single expressions in `{{ }}` interpolation
- [ ] Use ternary operators instead of if/else statements
- [ ] Move complex logic to computed properties or methods
- [ ] Avoid variable declarations in templates
- [ ] Use `v-if`/`v-else` directives for conditional rendering
**Incorrect:**
```vue
{{ var greeting = 'Hello' }}
{{ let x = 1 }}
{{ const name = 'Vue' }}
{{ if (ok) { return message } }}
{{ if (user) return user.name }}
{{ count++; return count }}
{{ items.push(newItem); items.length }}
{{ for (let i = 0; i < 5; i++) { } }}
```
**Correct:**
```vue
{{ message }}
{{ count + 1 }}
{{ items.length }}
{{ ok ? 'YES' : 'NO' }}
{{ user ? user.name : 'Guest' }}
{{ score >= 60 ? 'Pass' : 'Fail' }}
{{ formatDate(date) }}
{{ items.filter(i => i.active).length }}
{{ message.split('').reverse().join('') }}
{{ `Hello, ${name}!` }}
{{ { name: 'Vue', version: 3 } }}
```
## Use Directives for Control Flow
```vue
Welcome, {{ user.name }}!
Please log in
This toggles visibility
```
## Reference
- [Vue.js Template Syntax - Using JavaScript Expressions](https://vuejs.org/guide/essentials/template-syntax.html#using-javascript-expressions)
- [Vue.js Conditional Rendering](https://vuejs.org/guide/essentials/conditional.html)