Skip to content

Vue.js 2 templates and interpolations

Vue.js uses a templating language that's a superset of HTML. Any HTML is a valid Vue.js template, and in addition to that, Vue.js provides two powerful things: interpolation and directives.

Vue.js uses a templating language that’s a superset of HTML.

Any HTML is a valid Vue.js template, and in addition to that, Vue.js provides two powerful things: interpolation and directives.

This is a valid Vue.js template:

<span>Hello!</span>

This template can be put inside a Vue component declared explicitly:

new Vue({
  template: '<span>Hello!</span>'
})

or it can be put into a Single File Component:

<template>
  <span>Hello!</span>
</template>

This first example is very basic. The next step is making it output a piece of the component state, for example, a name.

This can be done using interpolation. First, we add some data to our component:

new Vue({
  data: {
    name: 'Flavio'
  },
  template: '<span>Hello!</span>'
})

and then we can add it to our template using the double brackets syntax:

new Vue({
  data: {
    name: 'Flavio'
  },
  template: '<span>Hello {{name}}!</span>'
})

One interesting thing here. See how we just used name instead of this.data.name?

This is because Vue.js does some internal binding and lets the template use the property as if it was local. Pretty handy.

In a single file component, that would be:

<template>
  <span>Hello {{name}}!</span>
</template>

<script>
export default {
  data() {
    return {
      name: 'Flavio'
    }
  }
}
</script>

I used a regular function in my export. Why not an arrow function?

This is because in data we might need to access a method in our component instance, and we can’t do that if this is not bound to the component, so arrow functions usage is not possible.

We could use an arrow function, but then I would need to remember to switch to a regular function in case I use this. Better play it safe, I think.

Now, back to the interpolation.

{{ name }} reminds of Mustache / Handlebars template interpolation, but it’s just a visual reminder.

While in those templating engines they are “dumb”, in Vue, you can do much more, it’s more flexible.

You can use any JavaScript expression inside your interpolations, but you’re limited to just one expression:

{{ name.reverse() }}
{{ name === 'Flavio' ? 'Flavio' : 'stranger' }}

Vue provides access to some global objects inside templates, including Math and Date, so you can use them:

{{ Math.sqrt(16) * Math.random() }}

It’s best to avoid adding complex logic to templates, but the fact Vue allows it gives us more flexibility, especially when trying things out.

We can first try to put an expression in the template, and then move it to a computed property or method later on.

The value included in any interpolation will be updated upon a change of any of the data properties it relies on.

You can avoid this reactivity by using the v-once directive.

The result is always escaped, so you can’t have HTML in the output.

If you need to have an HTML snippet you need to use the v-html directive instead.


→ Get my Vue.js 2 Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about vue: