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: docs/1-essentials/02-views.md
+58Lines changed: 58 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -577,6 +577,64 @@ For instance, the snippet below implements a tab component that accepts any numb
577
577
</x-tabs>
578
578
```
579
579
580
+
### Define slot ownership in nested view components
581
+
582
+
You can use `<x-slot name="mySlot" />` interchangeably to both *define* a slot with optional default content, or to provide content to *populate* the slot. Tempest will consider the hierarchy of the components from the AST to automatically detect your intent, however in more complex, especially nested, view components this can result in unexpected behaviour.
583
+
584
+
To override this behaviour and manually control in which view component your slots are considered to be *defined*, you can use `<x-slot define="mySlot" />` syntax instead. This causes the slot to be registered against the view component in which the keyword 'define' is used, instead of where the `slot` itself appears in the AST.
585
+
586
+
#### Extendable view component example using `define`
587
+
588
+
Let us assume you have an `x-container` view component, which is a `<div>` with formatting to act as a flex container for responsive sizing. You use this component repeatedly across your project, and it's effectively a macro to open and close the `<div>`; it doesn't have any slots or do anything special itself otherwise, with only a default `<x-slot/>` to render whatever it is given.
Now, assume we have an `x-header` in which we wish to use the `x-container`. Our `x-header` wishes to place slots `left` and `right` inside it; `x-header` owns these slots and wishes to expose these slots at the callsite in case they need custom content. Using the `define` keyword tells Tempest to treat these slots as *defined* by `x-header` instead of as a *slot to fill* inside `x-container`. Using `name` here instead of `define` would mean that Tempest falls back to the AST, and treats them as if they are slots of `<x-container>`.
593
+
```html x-header.view.php
594
+
<header>
595
+
<x-slotname="top" />
596
+
<x-container>
597
+
<x-slotdefine="left"> <!-- define this slot as a slot of x-header, compiled and passed into x-container's default slot -->
598
+
<x-header-left />
599
+
</x-slot>
600
+
<div>
601
+
I am in the center
602
+
</div>
603
+
<x-slotdefine="right"> <!-- define this slot as a slot of x-header, compiled and passed into x-container's default slot -->
604
+
<x-header-right />
605
+
</x-slot>
606
+
</x-container>
607
+
<x-slotname="bottom" />
608
+
</header>
609
+
```
610
+
At the callsite, you still use the `name` attribute to define which slot you're placing content into:
611
+
```html callsite.view.php
612
+
<x-header>
613
+
<x-slotname="left">Some content I want to insert</x-slot>
614
+
</x-header>
615
+
```
616
+
This example would replace the default content `<x-header-left />` instead with the literal string `Some content I want to insert` - or whatever you provide.
617
+
618
+
#### Populating a child's name slot using `define`
619
+
620
+
You can also push content into a child's named slot, not just the default slot, by creating a `define`d slot as follows:
Again, the `define` keyword registers a `left` slot against the view component `<x-outer>` irrespective of it's position in the AST, and means that at the callsite:
631
+
```html outercallsite.view.php
632
+
<x-outer>
633
+
<x-slotname="left">My override</x-slot>
634
+
</x-outer>
635
+
```
636
+
And so, this places the literal string `My override` into `<x-innner>`'s `left` slot.
637
+
580
638
### Dynamic view components
581
639
582
640
On some occasions, you might want to dynamically render view components, for example, render a view component whose name is determined at runtime. You can use the `{html}<x-component :is="">` element to do so:
0 commit comments