Skip to main content
Define captures a component subtree as a named template. Use references it by name, optionally injecting scoped data. The template is defined once and can appear any number of times in the tree with different data.

Basic Usage

Three cards with different data, but the card structure is defined once. The kwargs passed to Use become interpolation values scoped to that instance of the template.

Define

Define uses the context manager like any container, but it does not attach itself to a parent — it lives outside the component tree. Pass it to UIResponse via the defs parameter.
If a Define contains multiple children, they’re automatically wrapped in a Column.

Use

Use references a Define by name. Any kwargs that aren’t base component fields (css_class) become scoped interpolation values for the template.

With ForEach

Inside a ForEach, each item’s fields become the interpolation context automatically — so Use doesn’t need explicit overrides:

API Reference

Define Parameters

str
required
Template name, referenced by Use. Can be passed as a positional argument.

Use Parameters

str
required
The template name to reference (must match a Define name). Can be passed as a positional argument.
Any
Scoped interpolation values. Any kwarg that isn’t css_class becomes a scoped state override for the template.
str | None
default:"None"
Additional Tailwind CSS classes.

Protocol Reference

Define serializes to the template body in the defs envelope:
defs
Use without overrides serializes to a $ref node:
Use (bare)
With overrides, Use adds let bindings to the $ref node:
Use (with overrides)