Skip to content

Commit de0cdeb

Browse files
author
ntepluhina
committed
fix: fixed template syntax
1 parent 39c7432 commit de0cdeb

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/guide/template-syntax.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@ If you are familiar with Virtual DOM concepts and prefer the raw power of JavaSc
1212

1313
The most basic form of data binding is text interpolation using the "Mustache" syntax (double curly braces):
1414

15-
``` html
15+
```html
1616
<span>Message: {{ msg }}</span>
1717
```
1818

1919
The mustache tag will be replaced with the value of the `msg` property on the corresponding data object. It will also be updated whenever the data object's `msg` property changes.
2020

2121
You can also perform one-time interpolations that do not update on data change by using the [v-once directive](TODO:../api/#v-once), but keep in mind this will also affect any other bindings on the same node:
2222

23-
``` html
23+
```html
2424
<span v-once>This will never change: {{ msg }}</span>
2525
```
2626

2727
### Raw HTML
2828

2929
The double mustaches interprets the data as plain text, not HTML. In order to output real HTML, you will need to use the [`v-html` directive](TODO:../api/#v-html):
3030

31-
``` html
31+
```html
3232
<p>Using mustaches: {{ rawHtml }}</p>
3333
<p>Using v-html directive: <span v-html="rawHtml"></span></p>
3434
```
35+
3536
<template-1/>
3637

3738
The contents of the `span` will be replaced with the value of the `rawHtml` property, interpreted as plain HTML - data bindings are ignored. Note that you cannot use `v-html` to compose template partials, because Vue is not a string-based templating engine. Instead, components are preferred as the fundamental unit for UI reuse and composition.
@@ -44,13 +45,13 @@ Dynamically rendering arbitrary HTML on your website can be very dangerous becau
4445

4546
Mustaches cannot be used inside HTML attributes. Instead, use a [`v-bind` directive](../api/#v-bind):
4647

47-
``` html
48+
```html
4849
<div v-bind:id="dynamicId"></div>
4950
```
5051

5152
In the case of boolean attributes, where their mere existence implies `true`, `v-bind` works a little differently. In this example:
5253

53-
``` html
54+
```html
5455
<button v-bind:disabled="isButtonDisabled">Button</button>
5556
```
5657

@@ -60,19 +61,16 @@ If `isButtonDisabled` has the value of `null`, `undefined`, or `false`, the `dis
6061

6162
So far we've only been binding to simple property keys in our templates. But Vue.js actually supports the full power of JavaScript expressions inside all data bindings:
6263

63-
``` html
64-
{{ number + 1 }}
65-
66-
{{ ok ? 'YES' : 'NO' }}
67-
68-
{{ message.split('').reverse().join('') }}
64+
```html
65+
{{ number + 1 }} {{ ok ? 'YES' : 'NO' }} {{ message.split('').reverse().join('')
66+
}}
6967

7068
<div v-bind:id="'list-' + id"></div>
7169
```
7270

7371
These expressions will be evaluated as JavaScript in the data scope of the owner Vue instance. One restriction is that each binding can only contain **one single expression**, so the following will **NOT** work:
7472

75-
``` html
73+
```html
7674
<!-- this is a statement, not an expression: -->
7775
{{ var a = 1 }}
7876

@@ -88,7 +86,7 @@ Template expressions are sandboxed and only have access to a [whitelist of globa
8886

8987
Directives are special attributes with the `v-` prefix. Directive attribute values are expected to be **a single JavaScript expression** (with the exception of `v-for`, which will be discussed later). A directive's job is to reactively apply side effects to the DOM when the value of its expression changes. Let's review the example we saw in the introduction:
9088

91-
``` html
89+
```html
9290
<p v-if="seen">Now you see me</p>
9391
```
9492

@@ -98,15 +96,15 @@ Here, the `v-if` directive would remove/insert the `<p>` element based on the tr
9896

9997
Some directives can take an "argument", denoted by a colon after the directive name. For example, the `v-bind` directive is used to reactively update an HTML attribute:
10098

101-
``` html
99+
```html
102100
<a v-bind:href="url"> ... </a>
103101
```
104102

105103
Here `href` is the argument, which tells the `v-bind` directive to bind the element's `href` attribute to the value of the expression `url`.
106104

107105
Another example is the `v-on` directive, which listens to DOM events:
108106

109-
``` html
107+
```html
110108
<a v-on:click="doSomething"> ... </a>
111109
```
112110

@@ -116,7 +114,7 @@ Here the argument is the event name to listen to. We will talk about event handl
116114

117115
It is also possible to use a JavaScript expression in a directive argument by wrapping it with square brackets:
118116

119-
``` html
117+
```html
120118
<!--
121119
Note that there are some constraints to the argument expression, as explained
122120
in the "Dynamic Argument Expression Constraints" section below.
@@ -128,7 +126,7 @@ Here `attributeName` will be dynamically evaluated as a JavaScript expression, a
128126

129127
Similarly, you can use dynamic arguments to bind a handler to a dynamic event name:
130128

131-
``` html
129+
```html
132130
<a v-on:[eventName]="doSomething"> ... </a>
133131
```
134132

@@ -142,7 +140,7 @@ Dynamic arguments are expected to evaluate to a string, with the exception of `n
142140

143141
Dynamic argument expressions have some syntax constraints because certain characters, such as spaces and quotes, are invalid inside HTML attribute names. For example, the following is invalid:
144142

145-
``` html
143+
```html
146144
<!-- This will trigger a compiler warning. -->
147145
<a v-bind:['foo' + bar]="value"> ... </a>
148146
```
@@ -151,7 +149,7 @@ The workaround is to either use expressions without spaces or quotes, or replace
151149

152150
When using in-DOM templates (templates directly written in an HTML file), you should also avoid naming keys with uppercase characters, as browsers will coerce attribute names into lowercase:
153151

154-
``` html
152+
```html
155153
<!--
156154
This will be converted to v-bind:[someattr] in in-DOM templates.
157155
Unless you have a "someattr" property in your instance, your code won't work.
@@ -163,32 +161,32 @@ Unless you have a "someattr" property in your instance, your code won't work.
163161

164162
Modifiers are special postfixes denoted by a dot, which indicate that a directive should be bound in some special way. For example, the `.prevent` modifier tells the `v-on` directive to call `event.preventDefault()` on the triggered event:
165163

166-
``` html
167-
<form v-on:submit.prevent="onSubmit"> ... </form>
164+
```html
165+
<form v-on:submit.prevent="onSubmit">...</form>
168166
```
169167

170-
You'll see other examples of modifiers later, [for `v-on`](TODO:events.html#Event-Modifiers) and [for `v-model`](TODO:forms.html#Modifiers), when we explore those features.
168+
You'll see other examples of modifiers later, [for `v-on`](events.md#event-modifiers) and [for `v-model`](forms.md#modifiers), when we explore those features.
171169

172170
## Shorthands
173171

174172
The `v-` prefix serves as a visual cue for identifying Vue-specific attributes in your templates. This is useful when you are using Vue.js to apply dynamic behavior to some existing markup, but can feel verbose for some frequently used directives. At the same time, the need for the `v-` prefix becomes less important when you are building a [SPA](https://en.wikipedia.org/wiki/Single-page_application), where Vue manages every template. Therefore, Vue provides special shorthands for two of the most often used directives, `v-bind` and `v-on`:
175173

176174
### `v-bind` Shorthand
177175

178-
``` html
176+
```html
179177
<!-- full syntax -->
180178
<a v-bind:href="url"> ... </a>
181179

182180
<!-- shorthand -->
183181
<a :href="url"> ... </a>
184182

185-
<!-- shorthand with dynamic argument (2.6.0+) -->
183+
<!-- shorthand with dynamic argument -->
186184
<a :[key]="url"> ... </a>
187185
```
188186

189187
### `v-on` Shorthand
190188

191-
``` html
189+
```html
192190
<!-- full syntax -->
193191
<a v-on:click="doSomething"> ... </a>
194192

@@ -199,4 +197,4 @@ The `v-` prefix serves as a visual cue for identifying Vue-specific attributes i
199197
<a @[event]="doSomething"> ... </a>
200198
```
201199

202-
They may look a bit different from normal HTML, but `:` and `@` are valid characters for attribute names and all Vue-supported browsers can parse it correctly. In addition, they do not appear in the final rendered markup. The shorthand syntax is totally optional, but you will likely appreciate it when you learn more about its usage later.
200+
They may look a bit different from normal HTML, but `:` and `@` are valid characters for attribute names and all Vue-supported browsers can parse it correctly. In addition, they do not appear in the final rendered markup. The shorthand syntax is totally optional, but you will likely appreciate it when you learn more about its usage later.

0 commit comments

Comments
 (0)