# Using {{yield}} for multiple nested components

**URL:** <https://discuss.emberjs.com/t/using-yield-for-multiple-nested-components/9126>\
**Category:** Uncategorized\
**Created:** [November 18, 2015, 1:06am UTC](https://discuss.emberjs.com/t/using-yield-for-multiple-nested-components/9126 "2015-11-18T01:06:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bttf](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bttf/32/9898_2.png) [@bttf](https://discuss.emberjs.com/u/bttf)\
**Post date:** [November 18, 2015, 1:06am UTC](https://discuss.emberjs.com/t/using-yield-for-multiple-nested-components/9126/1 "2015-11-18T01:06:47Z")

</div>

If a component has two or three levels of descendant components, it can get a little annoying when having to pass down a parameter that is only needed by the last descendant all the way through the chain.

Using the `{{yield}}` helper can eliminate some of the cruft by allowing me to declare the nested component within the scope of the parent container. The only problem I am having now is that I cannot have multiple yield ‘outlets’ … If I wanted to nest multiple components in different parts of the containing component’s template I would be unable to (AFAIK).

The new contextual component feature in 2.3 seems like it might make things easier with the `(hash` and `(component` helpers but I am still limited by only having one `{{yield}}` “outlet” in a template. Does anyone have advice for this predicament and or a better way of seeing things? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![lightblade](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lightblade/32/15632_2.png) [@lightblade](https://discuss.emberjs.com/u/lightblade)\
**Post date:** [November 18, 2015, 1:17am UTC](https://discuss.emberjs.com/t/using-yield-for-multiple-nested-components/9126/2 "2015-11-18T01:17:46Z")

</div>

Well, you can do 2 with `{{else}}` and `{{yield inverse=true}}` 😄

Probably want to take a closer look at how Ember work with `{{else if ...}}`

---

<div class="post-metadata">

**Author:** ![sukima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/sukima/32/5773_2.png) [@sukima](https://discuss.emberjs.com/u/sukima)\
**Post date:** [June 8, 2018, 8:19pm UTC](https://discuss.emberjs.com/t/using-yield-for-multiple-nested-components/9126/4 "2018-06-08T20:19:32Z")

</div>

Apologies for resurrecting this thread but things have changed significantly since.

It is possible now to yield different sections in a single component. Your wrapping component will yield a specially crafted hash which references a dummy component.

For example the dummy component `foo-content`:

```nohighlight
{{yield}}

```

And an example wrapper component `foo-wrapper`:

```nohighlight
<div class="foo-wrapper">
  <div class="foo-wrapper__foo-a">
    {{yield (hash a=(component "foo-content"))}}
  </div>
  <div class="foo-wrapper__foo-b">
    {{yield (hash b=(component "foo-content"))}}
  </div>
  <div class="foo-wrapper__foo-c">
    {{yield (hash c=(component "foo-content"))}}
  </div>
</div>

```

Would provide you (the consumer) with this syntax:

```nohighlight
{{#foo-wrapper as |foo|}}
  {{#foo.a}}This is the Foo A component{{/foo.a}}
  {{#foo.b}}This is the Foo B component{{/foo.b}}
  {{#foo.c}}This is the Foo C component{{/foo.c}}
{{/foo-wrapper}}

```

[Here is a working demo](https://ember-twiddle.com/8c266f53719259ed8061b6eab92d00df).

---

<div class="post-metadata">

**Author:** ![NindroidX](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nindroidx/32/14046_2.png) [@NindroidX](https://discuss.emberjs.com/u/NindroidX)\
**Post date:** [June 15, 2018, 1:33pm UTC](https://discuss.emberjs.com/t/using-yield-for-multiple-nested-components/9126/5 "2018-06-15T13:33:49Z")

</div>

Has this been documented somewhere? The multiple yield values get merged into one object? I guess there is no way to know when one of the sub components received content?

---

<div class="post-metadata">

**Author:** ![sukima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/sukima/32/5773_2.png) [@sukima](https://discuss.emberjs.com/u/sukima)\
**Post date:** [June 15, 2018, 3:45pm UTC](https://discuss.emberjs.com/t/using-yield-for-multiple-nested-components/9126/6 "2018-06-15T15:45:12Z")

</div>

> Has this been documented somewhere?

I don’t think the guides discuss this but based on how contextual components work this is allowed by modern Ember. There have been blog posts and talks describing contextual components and this is just another (although unique) way of using them.

> The multiple yield values get merged into one object?

I don’t understand what you mean here nor how the code above resulted in that conclusion.

> I guess there is no way to know when one of the sub components received content?

Again I am not understanding what your asking here. Perhaps I don’t know your mental model of the contextual components to extrapolate meaning in this case. Sorry.

---

<div class="post-metadata">

**Author:** ![sukima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/sukima/32/5773_2.png) [@sukima](https://discuss.emberjs.com/u/sukima)\
**Post date:** [June 15, 2018, 4:04pm UTC](https://discuss.emberjs.com/t/using-yield-for-multiple-nested-components/9126/7 "2018-06-15T16:04:16Z")

</div>

For clarity, The code sample I gave basically has three yields. In normal components this would result in the content being yielded to render three times:

```nohighlight
{{! my-component/template.hbs }}
{{yield}}
{{yield}}
{{yield}}

{{! my-controller/template.hbs }}
{{#my-component}}
  <p>foo</p>
{{/my-component}}

{{! Would result in: }}
  <p>foo</p>
  <p>foo</p>
  <p>foo</p>

```

In the example the content is wrapped in components provided by the contextual component. In each instance of a yield a hash is provided with a unique key. In truth all three yields render the whole context except that if the key is `undefined` Ember renders nothing. Which means each yield will result in only one set of content rendered.

One could argue this is a hack. But it works and does not stray from the intent of contextual components. For contrast given the following:

```nohighlight
{{#my-component as |foo|}}
  <p>{{#foo.b}}foo{{/foo.b}}</p>
{{/my-component}}

```

would result in this output:

```nohighlight
<div>
  <p><!----></p>
  <p>foo</p>
  <p><!----></p>
</div>

```

Hopefully that clears up some confusion.

---

<div class="post-metadata">

**Author:** ![ppcano](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ppcano/32/14197_2.png) [@ppcano](https://discuss.emberjs.com/u/ppcano)\
**Post date:** [September 20, 2018, 12:41pm UTC](https://discuss.emberjs.com/t/using-yield-for-multiple-nested-components/9126/8 "2018-09-20T12:41:06Z")

</div>

This question has also be asked in [Stackoverflow](https://stackoverflow.com/questions/30282377/how-can-i-yield-multiple-pieces-of-content-into-an-ember-js-component-template/52425119#52425119).

Now, there is an accepted RFC for this problem; the [Named Templates Block API](https://github.com/emberjs/rfcs/blob/master/text/0226-named-blocks.md) will support passing multiple blocks to a component.

I also end up using `Contextual components` and created a similar [working demo](https://ember-twiddle.com/5141222076cbc62c4eab8493c71a65fb) that allows using the default subcomponent content or overwrites it.

```auto
{{my-component}}

{{#my-component as |f|}}
  {{f.header}}
  {{#f.content}}
    Custom content
  {{/f.content}}
  {{f.footer}}
{{/my-component}}

{{#my-component as |f|}}
  {{#f.header}}
     Custom header
  {{/f.header}}
  {{#f.content}}
     Custom content
  {{/f.content}}
  {{#f.footer}}
     Custom footer
  {{/f.footer}}
{{/my-component}}

```
