Skip to content

Commit 15fc152

Browse files
authored
Merge pull request #1958 from Shishouille/Shishouille-patch-1
Update block-content.md [Named blocks]
2 parents 92868b7 + 9f10eff commit 15fc152

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

guides/release/components/block-content.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,98 @@ 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+
{{#if this.isOpen}}
341+
<div class="popover__content">
342+
{{yield to="content"}}
343+
</div>
344+
{{/if}}
345+
</div>
346+
```
347+
348+
Without named blocks, we would certainly have to pass components as `args` to the popover. But this is much more practical!
349+
350+
Here’s how we would call our named blocks as a consumer:
351+
352+
```handlebars
353+
<Popover>
354+
<:trigger as |open|>
355+
<button type="button">Click to {{if open "close" "open"}} the popover!</button>
356+
</:trigger>
357+
<:content>
358+
This is what is shown when I'm opened!
359+
</:content>
360+
</Popover>
361+
```
362+
363+
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.
364+
365+
Rendering the previous code example would give this as result:
366+
367+
```html
368+
<!-- rendered -->
369+
<div class="popover">
370+
<div class="popover__trigger">
371+
<button type="button">Click to close the popover!</button>
372+
</div>
373+
<div class="popover__content">
374+
This is what is showed when I'm opened!
375+
</div>
376+
</div>
377+
```
378+
379+
Don't worry, you can also still use `yield` by itself, and mix it with named blocks. Let’s take a card example:
380+
381+
```handlebars {data-filename=app/components/card.hbs}
382+
<div class="card">
383+
{{#if (has-block "title")}}
384+
<div class="card__title">
385+
{{yield to="title"}}
386+
</div>
387+
{{/if}}
388+
<div class="card__content">
389+
{{yield}}
390+
</div>
391+
</div>
392+
```
393+
394+
A yielded block without a name is called `default`. So to access it, it’s like any other named blocks.
395+
396+
```handlebars
397+
<Card>
398+
<:title>
399+
<h3>It's nice to have me. Sometimes</h3>
400+
</:title>
401+
<:default>
402+
The card content will appear here!
403+
</:default>
404+
</Card>
405+
```
406+
407+
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:
408+
409+
```handlebars
410+
<Card>
411+
I don't want any title, and I only have a default content!
412+
</Card>
413+
```
414+
415+
As you are not using named blocks, you can simply yield the content you would like to add, which becomes the default yield block.
416+
417+
418+
325419
<!-- eof - needed for pages that end in a code block -->

0 commit comments

Comments
 (0)