Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 87 ยท Template Syntax

Text Interpolation

Learn Vue text interpolation with double curly braces, JavaScript expressions in templates, the globals whitelist, v-text, v-once and why v-html can cause XSS.

The most basic thing a template does is show data as text. In Vue you put the data between double curly braces, a form called mustache syntax (because {{ }} looks like a sideways mustache). Whenever the data changes, the text on the page changes too.

This lesson covers what you may write between the braces, what Vue does to the result, the directives v-text and v-once, and the dangerous but sometimes necessary v-html.

Mustache syntax

HTML
<p>Hello, {{ name }}!</p>

Vue replaces {{ name }} with the current value of name, converted to a string, and keeps it up to date. The same property can appear any number of times in a template. Mustaches work only for text: you cannot use them inside an attribute such as href. Attributes use v-bind, which is the next lesson.

JavaScript expressions

Between the braces you can write any single JavaScript expression: something that produces a value. Property access, arithmetic, method calls, the ternary operator and string concatenation are all fine:

Expressions inside mustaches

Notice what the template does not contain: no .value. Templates unwrap refs automatically, so you write price, not price.value. (The .value rule is the subject of Reactive State with ref.)

Expressions, not statements

The braces accept one expression only. These do not work:

HTML
<!-- statements are not allowed -->
{{ if (ok) { return "yes" } }}
{{ let message = "hi" }}
{{ for (const t of tags) { } }}

Use the ternary operator for conditions, and move anything longer into a function or a computed property. A good rule is that if a template expression does not fit on one short line, it belongs in the script. Templates describe what to show, scripts decide how to compute it. Computed Properties is the right tool for derived values.

The globals whitelist

Template expressions can only reach a restricted list of JavaScript globals such as Math, Date, Number, String, Array, Object, JSON, Intl, parseInt and isNaN. Everything else is looked up on the component instance. That means window, document, localStorage and your own global variables are not visible from a template. If you need one, expose it from setup:

Whitelisted globals and a blocked one

You will see "no" for document (the whitelist hides it, and in a development build Vue prints a warning about it). Math, parseInt and JSON work, and origin works because setup returned it.

Calling functions in templates

A mustache can call a function you returned from setup, as money(price) did above. That is perfectly fine for formatting, but remember that the function runs every time the component re-renders. Keep these functions fast and free of side effects: they must not change data, make network requests or log in a loop. If the same expensive calculation is needed in several places, a computed property caches it (Computed Properties).

Showing literal braces with v-pre

If you are writing a page about Vue, or you need the text {{ }} to appear unchanged, mark the element with v-pre. Vue then skips compiling that element and everything inside it:

HTML
<p v-pre>Write {{ name }} to show a value.</p>

This renders the text Write {{ name }} to show a value. exactly as typed. It also makes Vue skip the element at mount time, which is a minor speed-up for large static blocks.

What gets displayed

Vue converts the value with these rules:

  • Strings and numbers appear as they are.
  • null and undefined render as an empty string.
  • Objects and arrays are shown as indented JSON.
  • The text is always escaped: < becomes &lt;, so user data can never inject markup through a mustache.
flowchart LR
    E["Expression<br/>{{ value }}"] --> C["Converted to a string"]
    C --> X["Escaped: angle brackets<br/>become HTML entities"]
    X --> N["Text node in the DOM"]

Mustaches always produce a text node, never elements. That is why they are safe for untrusted data.

v-text and v-once

v-text is a directive that sets the text content of an element. It is equivalent to a mustache inside that element, with one difference: it replaces the element's whole content, so it is mostly useful when you do not want to risk the raw {{ }} flashing in an in-DOM template. Mustache is shorter and used far more often.

v-once renders an element a single time and then never updates it, even if the data changes. It is a small performance tool for text that you know is fixed after the first render.

Raw HTML with v-html

Sometimes the data really is HTML, for example a blog post body generated by a trusted server. A mustache would show the tags as text. v-html inserts the string as real markup instead:

Mustache, v-text and v-html side by side

Click Increment: the live line changes, but the v-once line stays at 0.

Never use v-html with untrusted content. If the string comes from a user, a comment form, a URL parameter or a database that users can write to, an attacker can supply markup such as <img src="x" onerror="stealCookies()">, and the browser will run that code on your page. This attack is called cross-site scripting (XSS). Vue cannot protect you inside v-html, because inserting raw HTML is exactly what you asked for. If you must show user-supplied rich text, sanitize it first with a dedicated library (see Security in Vue), or avoid HTML and use a mustache.

Filters are gone: use functions or computed properties

Vue 2 had template filters: {{ name | uppercase }}. Vue 3 removed them, and the pipe character is now the plain bitwise OR operator. Everything filters did is done with ordinary JavaScript: call a function in the mustache, or compute the value once.

Replacing Vue 2 filters

Edit the text and all four lines update. replaceAll replaces every occurrence (plain replace with a string changes only the first); deleting a substring is the same call with an empty replacement. Put helpers you use in many components in a shared module and import them, or register one on app.config.globalProperties for the Options API.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which of these is allowed inside {{ }}?

    Only a single JavaScript expression is allowed. The ternary operator is an expression; if, let and for are statements.
  2. 2. You wrote {{ document.title }} in a template and nothing appears. Why?

    Template expressions can access only a small whitelist of globals plus properties of the component instance. window and document are not on it.
  3. 3. The value of message is the string '<b>hi</b>'. What does {{ message }} show?

    Mustache interpolation always produces escaped text. Use v-html only if you really need markup, and only with trusted content.
  4. 4. Why is v-html dangerous with user-provided strings?

    v-html sets innerHTML. Untrusted markup can run code in your page, so only use it with content you control or have sanitized.
  5. 5. What does v-once do?

    v-once marks an element and its children as static after the first render, even if the data they depend on changes.

Summary

  • {{ value }} shows data as text and updates automatically; you never write .value for refs in a template.
  • Between the braces you may write one JavaScript expression, but not statements such as if, let or for.
  • Templates can reach only a whitelist of globals (Math, Date, JSON, Intl, parseInt, ...), not window or document.
  • null and undefined render as empty, objects as JSON, and everything is HTML-escaped.
  • v-text sets text content; v-once renders once and never updates.
  • v-html inserts raw HTML and is an XSS risk with untrusted content.

Next: Binding Attributes