Properties
Component properties hold server-side state. When properties change, the server re-renders and the client automatically morphs the DOM. Type-annotated class attributes become instance properties.
Defining properties
Type-annotated attributes become component properties:
from interlace import Component
class UserCard(Component):
username: str = ""
score: int = 0
is_active: bool = False
tags: list = []
metadata: dict = {}
def inline_template(self):
return """
<div>
<h2>{{ username }}</h2>
<p>Score: {{ score }}</p>
</div>
"""
- Type hints required:
username: str - Default values optional:
score: int = 0 - Accessible in templates:
{{ username }} - State lives on the server, synced to client on each render
New to server-driven frameworks? Check out How It Works to understand how changing properties triggers server re-renders and DOM morphing.
Mutable default isolation
Lists, dicts, and other mutable defaults are automatically isolated between component instances:
No Manual Isolation Needed Unlike plain Python classes, Interlace automatically creates fresh copies of mutable defaults for each instance. Write
items: list = []without worrying about shared state.
Public vs private
Properties and methods are public by default. Prefix with _ to make them private:
class DataProcessor(Component):
# Public - exposed to templates and client
result: str = ""
status: str = "idle"
# Private - internal only, not exposed
_cache: dict = {}
_counter: int = 0
def process(self):
# Public method - can be called from template
self._internal_logic()
self.result = "Done"
def _internal_logic(self):
# Private method - internal use only
self._counter += 1
| Type | Exposed to Templates | Synced to Client |
|---|---|---|
result |
✓ Yes | ✓ Yes |
_cache |
✗ No | ✗ No |
process() |
✓ Callable | ✓ Yes |
_internal_logic() |
✗ No | ✗ No |
Python @property decorators
Use @property for computed values:
class UserProfile(Component):
first_name: str = ""
last_name: str = ""
@property
def full_name(self):
return f"{self.first_name} {self.last_name}".strip()
def inline_template(self):
return """
<div>
<input lace:model="first_name" />
<input lace:model="last_name" />
<p>Full name: {{ full_name }}</p>
</div>
"""
Computed properties are read-only and re-evaluated on each render.
Updating properties
Properties can be updated from templates in several ways:
Form inputs
<!-- Deferred (no modifier): syncs on the next action -->
<input lace:model="username" />
<!-- Live syncing - updates on every keystroke -->
<input lace:model.live="search" />
<!-- Debounced live updates (300ms default) -->
<input lace:model.live.debounce="email" />
See lace:model for all modifiers and options.
Type in each field and watch when the echo updates - instantly, after a pause, or on blur.
echo: (stays empty until you press Send)
echo:
echo:
echo:
class ModelModifiers(Component):
deferred: str = ""
live: str = ""
debounced: str = ""
on_blur: str = ""
def sync(self):
# No-op: clicking just opens a request - the deferred value rides along with it.
pass
def inline_template(self):
return """
<div class="...">
<div>
<label>default - deferred, syncs on the next action</label>
<input lace:model="deferred">
<button lace:click="sync">Send</button>
<p>echo: {{ deferred }} <span>(stays empty until you press Send)</span></p>
</div>
<div>
<label>.live - every keystroke</label>
<input lace:model.live="live">
<p>echo: {{ live }}</p>
</div>
<div>
<label>.live.debounce.500ms - after a pause</label>
<input lace:model.live.debounce.500ms="debounced">
<p>echo: {{ debounced }}</p>
</div>
<div>
<label>.blur - when the field loses focus</label>
<input lace:model.blur="on_blur">
<p>echo: {{ on_blur }}</p>
</div>
</div>
"""
Method calls
<button lace:click="increment">+1</button>
<button lace:click="reset">Reset</button>
def increment(self):
self.count += 1
def reset(self):
self.count = 0
Alpine.js magic property
For advanced use cases, directly set properties with Alpine's $lace magic:
<button @click="$lace.count = 5">Set to 5</button>
<div x-show="$lace.count > 10">High count!</div>
The $lace object provides access to all public component properties within Alpine expressions.
Type coercion
Values that reach the server from a template - form inputs, click arguments, inline expressions - arrive as strings or JSON literals. Interlace coerces them to real Python types, so your methods and properties get genuine ints, floats, bools, dates, and even model instances. It happens in two layers.
Literals in attributes
In an expression like lace:click="add(5)" or lace:click="active = true", each value is parsed as a literal:
- numbers -
5,3.14 - booleans -
true,false, andnull - strings - quoted (
'draft'/"draft"); bare words are strings too
<button lace:click="set_qty(5)">Five</button> {# the int 5 #}
<button lace:click="set_active(true)">On</button> {# the bool True #}
<button lace:click="set_tier('pro')">Pro</button> {# the str "pro" #}
Property assignment
When a value is assigned to a property - via lace:model or an inline prop = value - it's coerced to that property's declared type hint:
class Cart(Component):
quantity: int = 1
discount: Decimal = Decimal("0")
# lace:model="quantity" stores an int (not "3")
# lace:model="discount" stores a Decimal
Beyond primitives, the type hint also drives the richer types Interlace serializes - datetime, date, UUID, Decimal, and Django models (resolved by primary key).
Method arguments
Arguments to a method call are coerced by the parameter's type hint, so a numeric literal stays a number all the way to your method:
def add(self, n: int):
self.count += n # lace:click="add(5)" → n is 5, not "5"
A parameter with no annotation receives the value as-is (the literal type parsed from the template). Between the two layers, a typed signature means you rarely cast by hand.
Initialization
Use mount() for properties requiring logic:
class Dashboard(Component):
user_name: str = ""
recent_items: list = []
def mount(self):
# Runs once when component is created
self.user_name = self.request.user.username
self.recent_items = self.fetch_recent_items()
def fetch_recent_items(self):
return Item.objects.filter(
user=self.request.user
).order_by('-created_at')[:10]
Don't Initialize in Class Body Avoid expensive operations in default values. Use
mount()for database queries, API calls, or anything requiringself.request.