Skip to content

Commit b77b0fa

Browse files
committed
Merge pull request #257 from simplesmiler/patch-1
Add a draft of template caveats
2 parents 187c81f + 2ae3f74 commit b77b0fa

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/guide/components.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,39 @@ var MyComponent = Vue.extend({
135135

136136
The `el` option also requires a function value when used in `Vue.extend()`, for exactly the same reason.
137137

138-
### `is` attribute
138+
### Template Parsing
139139

140-
Some HTML elements, for example `<table>`, have restrictions on what elements can appear inside them. Custom elements that are not in the whitelist will be hoisted out and thus not render properly. In such cases you should use the `is` special attribute to indicate a custom element:
140+
Vue.js template engine is DOM-based and uses native parser that comes with the browser instead of providing a custom one. There are benefits to this approach when compared to string-based template engines, but there are also caveats. Templates have to be individually valid pieces of HTML. Some HTML elements have restrictions on what elements can appear inside them. Most common of these restrictions are:
141+
142+
- `a` can not contain other interactive elements (e.g. buttons and other links)
143+
- `li` should be a direct child of `ul` or `ol`, and both `ul` and `ol` can only contain `li`
144+
- `option` should be a direct child of `select`, and `select` can only contain `option` (and `optgroup`)
145+
- `table` can only contain `thead`, `tbody`, `tfoot` and `tr`, and these elements should be direct cildren of `table`
146+
- `tr` can only contain `th` and `td`, and these elements should be direct children of `tr`
147+
148+
In practive these restriction can cause unexpected behavior. Although in simple cases it might appear to work, you can not rely on custom elements being expanded before browser validation. E.g. `<my-select><option>...</option></my-select>` is not a valid template even if `my-select` component eventually expands to `<select>...</select>`.
149+
150+
Another consequence is that you can not use custom tags (including custom elements and special tags like `<component>`, `<template>` and `<partial>`) inside of `ul`, `select`, `table` and other elements with similar restrictions. Custom tags will be hoisted out and thus not render properly.
151+
152+
In case of a custom element you should use the `is` special attribute:
141153

142154
``` html
143155
<table>
144156
<tr is="my-component"></tr>
145157
</table>
146158
```
147159

160+
In case of a `<template>` inside of a `<table>` you should use `<tbody>`, as tables are allowed to have multiple `tbody`:
161+
162+
``` html
163+
<table>
164+
<tbody v-for="item in items">
165+
<tr>Even row</tr>
166+
<tr>Odd row</tr>
167+
</tbody>
168+
</table>
169+
```
170+
148171
## Props
149172

150173
### Passing Data with Props

0 commit comments

Comments
 (0)