Skip to content

Commit a07779b

Browse files
authored
Update block-content.md
Add documentation for named blocks
1 parent 3395d89 commit a07779b

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

guides/release/components/block-content.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,96 @@ We can yield back multiple values as well, separated by spaces.
322322
</BlogPost>
323323
```
324324

325+
## Named Blocks
326+
327+
If you want to yield content to different spots in the same component, you can use named blocks. You just need to specify a name for the yielded block, like this:
328+
329+
```handlebars
330+
{{yield to="somePlace"}}
331+
```
332+
333+
You could also want to pass some values. This is the same process as the default `yield`, but you just have to pass `to` as the last argument. An example would be the popover:
334+
335+
```handlebars {data-filename=app/components/popover.hbs}
336+
<div class="popover">
337+
<div class="popover__trigger">
338+
{{yield this.isOpen to="trigger"}}
339+
</div>
340+
<div class="popover__content">
341+
{{yield this.isOpen to="content"}}
342+
</div>
343+
</div>
344+
```
345+
346+
If we hadn’t named blocks, we would certainly have to pass components as `args` to the popover. But this is much more practical!
347+
348+
Here’s how we would call it:
349+
350+
```handlebars
351+
<Popover>
352+
<:trigger as |open|>
353+
<button>Click to {{if open "close" "open"}} the popover!</button>
354+
</:trigger>
355+
<:content>
356+
This is what is showed when I'm opened!
357+
</:content>
358+
</Popover>
359+
```
360+
361+
We know the state of the popover because we passed it as an argument to the `yield`. To access its value, use the block parameters at the named block scope. It will not be accessible at the `Popover` level, so if you want the value to be available for all the blocks, you will have to pass it for each of them.
362+
363+
That would give this result:
364+
365+
```html
366+
<!-- rendered -->
367+
<div class="popover">
368+
<div class="popover__trigger">
369+
<button>Click to open the popover!</button>
370+
</div>
371+
<div class="popover__content">
372+
This is what is showed when I'm opened!
373+
</div>
374+
</div>
375+
```
376+
377+
Don't worry, you can also still use `yield` by itself, and mix it with named blocks. Let’s take a card example:
378+
379+
```handlebars {data-filename=app/components/card.hbs}
380+
<div class="card">
381+
{{#if (has-block "title")}}
382+
<div class="card__title">
383+
{{yield to="title"}}
384+
</div>
385+
{{/if}}
386+
<div class="card__content">
387+
{{yield}}
388+
</div>
389+
</div>
390+
```
391+
392+
A yielded block without a name is called `default`. So to access it, it’s like any other named blocks.
393+
394+
```handlebars
395+
<Card>
396+
<:title>
397+
<h3>It's nice to have me. Sometimes</h3>
398+
</:title>
399+
<:default>
400+
The card content will appear here!
401+
</:default>
402+
</Card>
403+
```
404+
405+
The title being optional when you create a card, you can use the `(has-block)` helper with the named block by adding its name as a first parameter. That means you could also create this card:
406+
407+
```handlebars
408+
<Card>
409+
I don't want any title, and I only have a default content!
410+
</Card>
411+
```
412+
413+
As you are not using named blocks, you can simply yield the content you would like to add.
414+
415+
416+
325417
<!-- eof - needed for pages that end in a code block -->

0 commit comments

Comments
 (0)