refecator: move defaults applying back to ext, raise on passing Slot to Slot, and docs cleanup (#1214)

* refecator: move defaults applying back to ext, raise on passing Slot to Slot, and docs cleanup

* docs: fix typo
This commit is contained in:
Juro Oravec 2025-05-26 11:59:17 +02:00 committed by GitHub
parent bae0f28813
commit 55b1c8bc62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 317 additions and 147 deletions

View file

@ -640,15 +640,25 @@ Table.render(
)
```
Slot class can be instantiated with a function, a string, or from another
[`Slot`](../../../reference/api#django_components.Slot) instance:
Slot class can be instantiated with a function or a string:
```py
slot1 = Slot(lambda ctx: f"Hello, {ctx.data['name']}!")
slot2 = Slot("Hello, world!")
slot3 = Slot(slot1)
```
!!! warning
Passing a [`Slot`](../../../reference/api#django_components.Slot) instance to the `Slot`
constructor results in an error:
```py
slot = Slot("Hello")
# Raises an error
slot2 = Slot(slot)
```
### Rendering slots
**Python**
@ -713,12 +723,13 @@ html = slot()
When accessing slots from within [`Component`](../../../reference/api#django_components.Component) methods,
the [`Slot`](../../../reference/api#django_components.Slot) instances are populated
with extra metadata [`component_name`](../../../reference/api#django_components.Slot.component_name)
and [`slot_name`](../../../reference/api#django_components.Slot.slot_name).
with extra metadata [`component_name`](../../../reference/api#django_components.Slot.component_name),
[`slot_name`](../../../reference/api#django_components.Slot.slot_name), and
[`nodelist`](../../../reference/api#django_components.Slot.nodelist).
These are used solely for debugging.
These are used for debugging, such as printing the path to the slot in the component tree.
In fact, you can set these fields too when creating new slots:
When you create a slot, you can set these fields too:
```py
# Either at slot creation
@ -753,19 +764,6 @@ slot = Slot("Hello!")
print(slot.nodelist) # <django.template.Nodelist: ['Hello!']>
```
!!! info
If you pass a [`Slot`](../../../reference/api#django_components.Slot) instance to the constructor,
the inner slot will be "unwrapped" and its `Slot.contents` will be used instead.
```py
slot = Slot("Hello")
print(slot.contents) # "Hello"
slot2 = Slot(slot)
print(slot2.contents) # "Hello"
```
### Escaping slots content
Slots content are automatically escaped by default to prevent XSS attacks.