Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Template Syntax Rendering

Template Syntax Rendering

In this chapter you will learn to use Vue's template syntax to render JS data into HTML, making the blog home page display a list of article cards.


Interpolation Expression {{ }}

The interpolation expression is the most basic data binding method in Vue.

It "inserts" the value of a JS variable into HTML; the syntax is double curly braces wrapping the variable name.

Example

The page will render:



RUNOOB 前端笔记

作者:小明

当前时间:2024/5/15

Inside the braces you can put any JS expression: variables, operations, ternary expressions, function calls are all fine, but you cannot put statements (such as if, for).

The interpolation expression only replaces the text inside a tag, not the tag itself. For example,

{{ content }}

the `` in
tag itself is unaffected.


Attribute Binding v-bind (shorthand :)

The interpolation expression can only be used for tag content, not for HTML attributes.

To use a JS variable in an attribute, you need the v-bind directive.

Example

The shorthand colon is the most common form; in real development you almost never use v-bind: the full name.


List Rendering v-for

The blog home page needs to display multiple articles; one article is one object, and multiple articles are an array of objects.

v-for can loop over an array and generate the corresponding HTML for each element.

Example

The Importance of the key Attribute

:key is the essential companion of v-for; it tells Vue the identity of each element.

When the array order changes (e.g., insertion, deletion, sorting), Vue uses the key to determine which DOM nodes need updating and which can be reused.

Key Values Consequence
A unique id (recommended) Correctly track each element; efficient updates
Array index May cause state confusion on insert/delete (e.g., input content swapping)
Not writing key The console will warn, and Vue defaults to an "in-place update" strategy with poorer performance

Always add a unique and stable key to v-for; do not use index as the key. This is one of the most important habits in Vue development.


Conditional Rendering v-if / v-else

When you need to show different content under different conditions, use the v-if family of directives.

Example

This pattern covers the three most common UI states: loading → has data → empty data.

v-if vs v-show

Instruction Switching Method Initial Render Cost Switch Cost Applicable scenarios
v-if Add/remove DOM nodes Low (not rendered) High (rebuild DOM) Scenarios where conditions rarely change, such as permission control
v-show Toggle CSS display High (always rendered) Low (only change styles) Scenarios with frequent switching, such as tab switching

Simple rule: use v-show for frequent switching, v-if for stable conditions.


Hands-On: The Complete Blog Home Page

Combine the template syntax learned earlier into a complete blog home page component.

Example


Chapter Summary

In this chapter you learned four core template syntax features:{{ }} Interpolation to display data,v-bind(shorthand :) to bind attributes,v-for to loop-render a list,v-if/v-else to conditionally control visibility.

At this point, the blog home page can dynamically render article cards based on JS data.