Skip to content

Commit adba417

Browse files
phananchrisvfritz
authored andcommitted
docs: add mustaches vs v-html example (#1277)
* docs: add mustaches vs v-html example * docs: use <p> instead of <div>
1 parent 2ca4ab8 commit adba417

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/v2/guide/syntax.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,28 @@ You can also perform one-time interpolations that do not update on data change b
3333
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:
3434

3535
``` html
36-
<div v-html="rawHtml"></div>
36+
<p>Using mustaches: {{ rawHtml }}</p>
37+
<p>Using v-html directive: <span v-html="rawHtml"></span></p>
3738
```
3839

39-
The contents of this `div` 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.
40+
{% raw %}
41+
<div id="example1" class="demo">
42+
<p>Using mustaches: {{ rawHtml }}</p>
43+
<p>Using v-html directive: <span v-html="rawHtml"></span></p>
44+
</div>
45+
<script>
46+
new Vue({
47+
el: '#example1',
48+
data: function () {
49+
return {
50+
rawHtml: '<span style="color: red">This should be red.</span>'
51+
}
52+
}
53+
})
54+
</script>
55+
{% endraw %}
56+
57+
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.
4058

4159
<p class="tip">Dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to [XSS vulnerabilities](https://en.wikipedia.org/wiki/Cross-site_scripting). Only use HTML interpolation on trusted content and **never** on user-provided content.</p>
4260

0 commit comments

Comments
 (0)