# Interlace vs HTMX

HTMX and Interlace both put "HTML over the wire" at the centre of your app, but they work at
different layers. HTMX is the **transport**: it lets any HTML element make a request and swap the
response into the page. Interlace is a **component model** built on that same idea: stateful Python
components with actions, binding, and lifecycle, with the request and response plumbing handled for
you.

Neither is a lesser version of the other. Which one fits depends on how much of the interaction you
want to own by hand.

## The one-line version

- **HTMX** gives you a way to request HTML and swap it in. Everything behind that, state, routing,
  which template to render, is yours to design.
- **Interlace** gives you a component: typed state, methods, two-way binding, validation and polling,
  plus the morphing, all wired together. You write Python, not endpoints.

HTMX has no opinion about your backend. Interlace *is* the backend piece, which is exactly why it can
offer features that need server-managed state out of the box.

## Side by side

| | HTMX | Interlace |
|---|---|---|
| **Layer** | Client transport (request + swap) | Full component model (state + actions + transport) |
| **Backend opinion** | None, works with any language | Django-native |
| **State** | You manage it (DB, session, hidden inputs, URL) | Managed per component (typed properties, signed snapshots) |
| **Wiring a feature** | A URL + a view + a partial template each | A method on a component; no extra URLs |
| **Two-way binding** | Manual (`hx-post` + your endpoint) | `lace:model` with `.live` / `.debounce` |
| **Client interactivity** | Add Alpine or JS yourself | Alpine bundled; `lace:*` directives |
| **Real-time** | `hx-trigger="every 2s"`, you write the endpoint | `lace:poll`, batched onto one shared clock |
| **Validation** | Hand-rolled or a form round-trip | Django forms + `validate_only()` |
| **DOM updates** | Swap strategies you choose | Automatic morphing (preserves focus, scroll, Alpine state) |
| **Footprint / lock-in** | Tiny script, no lock-in | Larger surface: a component metaclass and state snapshots |
| **Best when** | You want minimal, explicit, full control, or you're not on Django | You want a component and state model with less plumbing |

## Where HTMX is the better call

For plenty of jobs HTMX is the simpler, lighter tool, and it's worth being honest about that.

- You want to **progressively enhance** server-rendered pages: a form post here, a lazy tab there.
- You want **total control** over exactly what HTML the server returns, and when.
- You value a **tiny dependency** with no framework lock-in, or you aren't on Django at all.
- The interaction is **stateless enough** that a plain view returning a partial is all you need.

If "make this button POST and swap in a partial" describes your problem, HTMX is hard to beat.

## Where the plumbing adds up

Because HTMX stays out of your backend, the moment an interaction becomes *stateful* you build the
state layer yourself. A live-validated multi-field form, a sortable list that persists its order, a
wizard, a search box with debounced results, each of these in HTMX means:

1. one or more URLs,
2. a view per interaction,
3. a partial template per fragment,
4. and somewhere to keep the state between requests.

That's fine once. It multiplies as the page gets richer. Interlace collapses it into a single
component: the state is typed attributes, the interactions are methods, and there are no extra URLs
or partials to keep in sync.

## The same feature, both ways

**HTMX + Django**, a counter needs a view, a URL, and a partial, and the state rides along in the
request:

```python
# views.py
def increment(request):
    count = int(request.POST.get("count", 0)) + 1
    return render(request, "_counter.html", {"count": count})
```

```html
<!-- _counter.html -->
<button hx-post="{% url 'increment' %}"
        hx-vals='{"count": {{ count }} }'
        hx-swap="outerHTML">
    Count: {{ count }}
</button>
```

**Interlace**, one component, and the state lives in Python between requests:

```python
class Counter(Component):
    count: int = 0

    def increment(self):
        self.count += 1

    def inline_template(self):
        return '<button lace:click="increment">Count: {{ count }}</button>'
```

```django
{% interlace "Counter" %}
```

No URL, no partial, no manual state threading.

## They're not enemies

Interlace builds on the same HTML-over-the-wire instinct HTMX popularised, and there's nothing
stopping you using HTMX alongside it for the pieces you'd rather drive by hand. Reach for HTMX when
you want the transport and full control; reach for Interlace when you want the component and state
model to come with it. For a longer, worked example see [HTMX + Django](htmx-and-django.md).
