Skip to content

Commit 7628105

Browse files
committed
{{>yield}}
1 parent 2429b37 commit 7628105

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/compiler.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ function Compiler (vm, options) {
116116
compiler.parseDeps()
117117

118118
// done!
119+
compiler.rawContent = null
119120
compiler.init = false
120121

121122
// post compile / ready hook
@@ -136,6 +137,13 @@ CompilerProto.setupElement = function (options) {
136137

137138
var template = options.template
138139
if (template) {
140+
// collect anything already in there
141+
/* jshint boss: true */
142+
var child,
143+
frag = this.rawContent = document.createDocumentFragment()
144+
while (child = el.firstChild) {
145+
frag.appendChild(child)
146+
}
139147
// replace option: use the first node in
140148
// the template directly
141149
if (options.replace && template.childNodes.length === 1) {
@@ -146,7 +154,6 @@ CompilerProto.setupElement = function (options) {
146154
}
147155
el = replacer
148156
} else {
149-
el.innerHTML = ''
150157
el.appendChild(template.cloneNode(true))
151158
}
152159
}
@@ -415,9 +422,18 @@ CompilerProto.compileTextNode = function (node) {
415422
if (token.key) { // a binding
416423
if (token.key.charAt(0) === '>') { // a partial
417424
partialId = token.key.slice(1).trim()
418-
partial = this.getOption('partials', partialId)
419-
if (partial) {
420-
el = partial.cloneNode(true)
425+
if (partialId === 'yield') {
426+
el = this.rawContent
427+
} else {
428+
partial = this.getOption('partials', partialId)
429+
if (partial) {
430+
el = partial.cloneNode(true)
431+
} else {
432+
utils.warn('Unknown partial: ' + partialId)
433+
continue
434+
}
435+
}
436+
if (el) {
421437
// save an Array reference of the partial's nodes
422438
// so we can compile them AFTER appending the fragment
423439
partialNodes = slice.call(el.childNodes)

test/functional/fixtures/template.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<div id="usa" v-partial="global"></div>
44
<div id="japan">{{> local}}</div>
55
<div id="repeat">{{> repeat}}</div>
6+
<div id="yielder"><yielder><p>{{a}}</p><p>{{b}}</p></yielder></div>
67

78
<script type="text/v-template" id="test">
89
<p>{{hi}}!</p>
@@ -12,6 +13,10 @@
1213
<p v-repeat="items">{{title}}</p>
1314
</script>
1415

16+
<script type="text/v-template" id="yield-template">
17+
<h1>before</h1>{{>yield}}<h2>after</h2>
18+
</script>
19+
1520
<script src="../../../dist/vue.js"></script>
1621
<script>
1722

@@ -61,4 +66,13 @@
6166
items: [{ title: 'Repeat' }]
6267
}
6368
})
69+
70+
Vue.component('yielder', {
71+
template: '#yield-template',
72+
data: {
73+
a: 'A', b: 'B'
74+
}
75+
})
76+
77+
new Vue({el:'#yielder'})
6478
</script>

test/functional/specs/template.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
casper.test.begin('Templates and Partials', 5, function (test) {
1+
casper.test.begin('Templates and Partials', 6, function (test) {
22

33
casper
44
.start('./fixtures/template.html')
@@ -8,6 +8,9 @@ casper.test.begin('Templates and Partials', 5, function (test) {
88
test.assertSelectorHasText('#china', '你好', 'direct option')
99
test.assertSelectorHasText('#hawaii', 'Aloha', 'extend option')
1010
test.assertSelectorHasText('#repeat', 'Repeat', 'inline partial with repeat')
11+
test.assertEvalEquals(function () {
12+
return document.querySelector('#yielder').innerHTML
13+
}, '<yielder><h1>before</h1><p>A</p><p>B</p><h2>after</h2></yielder>')
1114
})
1215
.run(function () {
1316
test.done()

0 commit comments

Comments
 (0)