You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/release/components/block-content.md
+94Lines changed: 94 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -322,4 +322,98 @@ We can yield back multiple values as well, separated by spaces.
322
322
</BlogPost>
323
323
```
324
324
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:
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
+
<divclass="popover">
370
+
<divclass="popover__trigger">
371
+
<buttontype="button">Click to close the popover!</button>
372
+
</div>
373
+
<divclass="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:
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
+
325
419
<!-- eof - needed for pages that end in a code block -->
0 commit comments