Module: Vue Instance and Template Syntax

Text interpolation

Vue Instance and Template Syntax: Text Interpolation

Vue.js allows you to declaratively bind data to the DOM using a powerful template syntax. One of the most fundamental aspects of this is text interpolation, which lets you display data directly within your HTML.

What is a Vue Instance?

Before diving into interpolation, it's crucial to understand the Vue instance. A Vue instance is the core of a Vue application. It's where you define your data, methods, and other options. You create an instance using the Vue() constructor.

const app = new Vue({
  // Options (data, methods, etc.)
  el: '#app', // The element in the DOM where the Vue instance will be mounted
  data: {
    message: 'Hello Vue!'
  }
});

In this example:

  • new Vue() creates a new Vue instance.
  • el: '#app' tells Vue to take control of the HTML element with the ID "app".
  • data: { message: 'Hello Vue!' } defines a data property called message with the initial value "Hello Vue!".

Template Syntax: Text Interpolation with {{ }}

Text interpolation uses double curly braces {{ }} to embed JavaScript expressions directly into your HTML template. Vue will evaluate these expressions and display the result as text.

Example:

HTML (index.html):

<div id="app">
  <p>{{ message }}</p>
</div>

JavaScript (app.js):

const app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  }
});

Output:

<div id="app">
  <p>Hello Vue!</p>
</div>

Vue replaces {{ message }} with the value of the message data property.

Interpolating Data Properties

You can interpolate any data property defined within the data object of your Vue instance.

const app = new Vue({
  el: '#app',
  data: {
    name: 'Alice',
    age: 30,
    isDeveloper: true
  }
});

HTML:

<div id="app">
  <p>Name: {{ name }}</p>
  <p>Age: {{ age }}</p>
  <p>Is Developer: {{ isDeveloper }}</p>
</div>

Output:

<div id="app">
  <p>Name: Alice</p>
  <p>Age: 30</p>
  <p>Is Developer: true</p>
</div>

Interpolating JavaScript Expressions

You're not limited to just data properties. You can also use JavaScript expressions within the double curly braces.

Example:

const app = new Vue({
  el: '#app',
  data: {
    number1: 10,
    number2: 5
  }
});

HTML:

<div id="app">
  <p>Sum: {{ number1 + number2 }}</p>
  <p>Product: {{ number1 * number2 }}</p>
</div>

Output:

<div id="app">
  <p>Sum: 15</p>
  <p>Product: 50</p>
</div>

Important Considerations:

  • Simple Expressions: Keep expressions within the template relatively simple. Complex logic should be handled in methods.
  • Side Effects: Avoid side effects (like assignments) within the interpolation. Vue is designed for declarative data binding, and side effects can lead to unpredictable behavior. For example, {{ someVariable = 'new value' }} is generally a bad practice.
  • String Literals: You can include string literals within the interpolation. For example: {{ 'Hello ' + name }}.
  • Filters: You can use filters to format the output of your data. (Covered in a separate topic).
  • Single Quotes: Use single quotes within the expression if the outer attribute uses double quotes, and vice-versa, to avoid syntax errors. For example: <p>{{ 'It's a beautiful day!' }}</p>.

Text interpolation is a cornerstone of Vue.js, enabling dynamic and reactive user interfaces. It's a simple yet powerful feature that allows you to easily display data and create engaging web applications.