Slots
The slot value and fill types.
Slot class
Bases: Generic
Normalized slot content: a lazy, repeatable, standalone callable.
Construct it from a string, a function, a CitryElement, a ComponentLike, or a CitryRender. Calling the Slot returns a render part; str(slot) renders and serializes in one step. A standalone Slot containing a ComponentLike cannot resolve without an active component render.
Example
::
Slot("Hello!") # static content Slot(lambda ctx: f"Hi {ctx.data.name}!") # content function Slot(Card(title="Hi")) # composed element
component_name attribute
Name of the component this slot content was given to (for debugging).
source_position attribute
The (start, end) span of the <c-fill> in its template, if any.
content_func attribute
content_func: SlotFunc[TSlotData]The content function. Call the Slot itself instead of calling this directly.
SlotContext class
Bases: Generic
The single argument a slot function receives.
Example
::
def my_slot(ctx: SlotContext) -> str: return f"Hello, {ctx.data.name}!"
data attribute
data: TSlotDataData passed to the slot by the <c-slot> tag (its extra attributes), or by the caller when the Slot is invoked directly. At runtime this is an immutable SlotData; the type parameter may describe a more precise component-specific field shape.
fallback attribute
fallback: Slot | NoneThe slot's fallback content (the body of the <c-slot> tag), as a Slot.
None when the Slot is called directly, outside a <c-slot> site. Coerce it to a string (or render it via {{ fallback }}) to render the fallback.
provides attribute
The provide/inject entries active where the Slot was invoked (the <c-slot> site or expression site). None when the Slot is called directly, outside a render. Template-defined fills use this so their bodies render with the invoking site's provides; a slot function may read it to inspect provided data.
SlotData class
Bases: Mapping
Immutable data passed from a slot outlet to its fill.
Identifier-like keys are available as attributes, while every key remains available through mapping access. Keys beginning with an underscore and keys that collide with mapping methods intentionally require bracket access or fill-data destructuring.
Example
::
data = SlotData({"label": "Save", "aria-label": "Save item"}) data.label data["aria-label"]
Parameters
SlotFunc class
Bases: Protocol
The signature of a slot content function.
Example
::
def header(ctx: SlotContext) -> str: if ctx.data.get("name"): return f"Hello, {ctx.data.name}!" return str(ctx.fallback)
SlotInput attribute
All forms in which slot content can be passed to a component.
Use this to type the fields of a component's Slots class::
class Table(Component):
class Slots:
header: SlotInput
footer: SlotInput[FooterSlotData]
A field without a default must be filled whenever the component is used. A field annotated as SlotInput | None with a None default is optional. The required attribute on <c-slot> checks something different: it raises an error only if Citry renders that tag without content.
SlotResult attribute
What a slot function may return.
A plain str is escaped when the slot renders; a SafeString or CitryRender is trusted and inlined as-is. A ComponentLike resolves against the Citry instance rendering the slot.