diff --git a/dev/concepts/fundamentals/slots/index.html b/dev/concepts/fundamentals/slots/index.html index bcdd5cea..c89e42d7 100644 --- a/dev/concepts/fundamentals/slots/index.html +++ b/dev/concepts/fundamentals/slots/index.html @@ -285,49 +285,47 @@ "footer": Slot(footer), }, ) -
Slot class can be instantiated with a function, a string, or from another Slot
instance:
Slot class can be instantiated with a function or a string:
slot1 = Slot(lambda ctx: f"Hello, {ctx.data['name']}!")
slot2 = Slot("Hello, world!")
-slot3 = Slot(slot1)
-
Python
You can render a Slot
instance by simply calling it with data:
Warning
Passing a Slot
instance to the Slot
constructor results in an error:
Optionally you can pass the fallback value to the slot. Fallback should be a string.
Template
Alternatively, you can pass the Slot
instance to the {% fill %}
tag:
If a slot function is rendered by the {% slot %}
tag, you can access the current Context using the context
attribute.
class Table(Component):
- template = """
- {% with "abc" as my_var %}
- {% slot "name" %}
- Hello!
- {% endslot %}
- {% endwith %}
- """
-
-def slot_func(ctx):
- return f"Hello, {ctx.context['my_var']}!"
-
-slot = Slot(slot_func)
-html = slot()
-
Warning
While available, try to avoid using the context
attribute in slot functions.
Instead, prefer using the data
and fallback
attributes.
Access to context
may be removed in future versions (v2, v3).
When accessing slots from within Component
methods, the Slot
instances are populated with extra metadata component_name
and slot_name
.
These are used solely for debugging.
In fact, you can set these fields too when creating new slots:
# Either at slot creation
-slot = Slot(
- lambda ctx: f"Hello, {ctx.data['name']}!",
- component_name="table",
- slot_name="name",
-)
-
-# Or later
-slot.component_name = "table"
-slot.slot_name = "name"
-
Whether you create a slot from a function, a string, or from the {% fill %}
tags, the Slot
class normalizes its contents to a function.
Use Slot.contents
to access the original value that was passed to the Slot constructor.
If the slot was created from a string or from the {% fill %}
tags, the contents will be accessible also as a Nodelist under Slot.nodelist
.
Info
If you pass a Slot
instance to the constructor, the inner slot will be "unwrapped" and its Slot.contents
will be used instead.
Slots content are automatically escaped by default to prevent XSS attacks.
In other words, it's as if you would be using Django's escape()
on the slot contents / result:
Python
You can render a Slot
instance by simply calling it with data:
slot = Slot(lambda ctx: f"Hello, {ctx.data['name']}!")
+
+# Render the slot with data
+html = slot({"name": "John"})
+
Optionally you can pass the fallback value to the slot. Fallback should be a string.
Template
Alternatively, you can pass the Slot
instance to the {% fill %}
tag:
If a slot function is rendered by the {% slot %}
tag, you can access the current Context using the context
attribute.
class Table(Component):
+ template = """
+ {% with "abc" as my_var %}
+ {% slot "name" %}
+ Hello!
+ {% endslot %}
+ {% endwith %}
+ """
+
+def slot_func(ctx):
+ return f"Hello, {ctx.context['my_var']}!"
+
+slot = Slot(slot_func)
+html = slot()
+
Warning
While available, try to avoid using the context
attribute in slot functions.
Instead, prefer using the data
and fallback
attributes.
Access to context
may be removed in future versions (v2, v3).
When accessing slots from within Component
methods, the Slot
instances are populated with extra metadata component_name
, slot_name
, and nodelist
.
These are used for debugging, such as printing the path to the slot in the component tree.
When you create a slot, you can set these fields too:
# Either at slot creation
+slot = Slot(
+ lambda ctx: f"Hello, {ctx.data['name']}!",
+ component_name="table",
+ slot_name="name",
+)
+
+# Or later
+slot.component_name = "table"
+slot.slot_name = "name"
+
Whether you create a slot from a function, a string, or from the {% fill %}
tags, the Slot
class normalizes its contents to a function.
Use Slot.contents
to access the original value that was passed to the Slot constructor.
If the slot was created from a string or from the {% fill %}
tags, the contents will be accessible also as a Nodelist under Slot.nodelist
.
Slots content are automatically escaped by default to prevent XSS attacks.
In other words, it's as if you would be using Django's escape()
on the slot contents / result:
from django.utils.html import escape
class Calendar(Component):
template = """
@@ -457,4 +455,4 @@
# ❌ Does not work
if self.is_filled["my super-slot :)"]:
# Do something
-