Reusable components with Cotton

Interlace gives you the reactive half, state and behaviour in a Python component. For the markup half, django-cotton lets you build reusable HTML tags (<c-button>, <c-field>) with slots and attributes. They compose: Cotton for presentational pieces you reuse, Interlace for the state that drives them.

Cotton is optional, it is not an Interlace dependency. Add it only for this workflow:

pip install django-cotton
# settings.py
INSTALLED_APPS = [..., "django_cotton", "interlace"]

Cotton expands <c-...> tags server-side, before Interlace renders, in a template_name file or in an inline_template() string (when django-cotton is installed, Interlace runs Cotton's compiler over inline sources too). <c-field lace:model="email"> then becomes a plain <input lace:model="email">, and binding, validation and morphing all behave exactly as if you had written the input by hand.

A reusable field

Most forms repeat the same label + input + error markup. Wrap it once as <c-field> and let Cotton's {{ attrs }} forward any attribute, lace:model, type, placeholder, straight to the input. The error prop is a normal {{ }} expression pulled from the component's errors dict:

PREVIEW

Bound value:

{# reusable field: {{ attrs }} forwards lace:model + any input attr #}
<c-vars label="" error="" />
<label class="block">
    <span class="text-sm font-medium">{{ label }}</span>
    <input class="w-full px-3 py-2 rounded border
           {% if error %}border-red-500{% else %}border-gray-300{% endif %}"
           {{ attrs }}>
    {% if error %}<span class="text-sm text-red-600">{{ error }}</span>{% endif %}
</label>
from django import forms
from interlace import Component

class FieldDemo(Component):
    email: str = ""
    template_name = "field_demo.html"   # a file template, so Cotton runs

    def rules(self):
        return {"email": forms.EmailField()}

    def updated(self, prop, value):
        self.validate_only(prop)
<c-field label="Email" lace:model.live="email"
         error="{{ errors.email.0 }}" />
<p>Bound value: {{ email }}</p>

{{ attrs }} is the whole trick: anything you put on <c-field> that it did not declare in <c-vars> lands on the <input>. That is why lace:model.live="email" binds normally, and why the same field serves emails, passwords or search boxes without touching field.html again.

Wrapping a third-party JS library

Some widgets, date pickers, maps, rich-text editors, own their DOM and must not be touched by morphing. Interlace has lace:ignore for exactly this: the element is skipped during morph, so the widget survives re-renders. Combine it with Cotton for a clean, reusable tag.

Here is a date field backed by flatpickr. Open it, pick a date, and the value flows back into Python state:

PREVIEW

Selected: (none)

{# wraps flatpickr; lace:ignore keeps morph from touching it #}
<c-vars name label="" />
<label class="block" x-data
       x-init="flatpickr($refs.input, {
           onChange: () => $refs.input.dispatchEvent(new Event('change'))
       })">
    <span class="text-sm font-medium">{{ label }}</span>
    <input x-ref="input" lace:ignore lace:model.change="{{ name }}"
           class="w-full px-3 py-2 rounded border" {{ attrs }}>
</label>
from interlace import Component

class DateDemo(Component):
    checkin: str = ""
    template_name = "date_demo.html"   # file template, so Cotton runs
<c-date-field label="Check-in" name="checkin" />
<p>Selected: {{ checkin }}</p>
  • lace:ignore stops the morpher re-rendering the input, so flatpickr's calendar and listeners survive when the rest of the component updates.

  • x-init initialises the library once; its onChange fires a native change event, so lace:model.change syncs the picked value back into Python state.

The same shape, lace:ignore + x-init + a bound input (or a method call), wraps almost any library: Quill / TinyMCE editors, Choices.js selects, Chart.js canvases, Leaflet maps.

Which tool for what

  • <c-...> (Cotton) handles reusable markup: fields, cards, buttons, layout. No state of its own.
  • Component (Interlace) handles state and behaviour: properties, methods, validation, polling.
  • lace:ignore protects a subtree (usually a third-party widget) from morphing.

Build presentational pieces with Cotton, pass them lace: attributes, and let Interlace drive the reactivity.

View this page as markdown - handy as context for your AI tools. Full index at /llms.txt.

Theme
Accent
Ink