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
+92Lines changed: 92 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -322,4 +322,96 @@ 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:
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
+
<divclass="popover">
368
+
<divclass="popover__trigger">
369
+
<button>Click to open the popover!</button>
370
+
</div>
371
+
<divclass="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:
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
+
325
417
<!-- eof - needed for pages that end in a code block -->
0 commit comments