# Templates

A component's template is HTML with `lace:` directives and ordinary Django template syntax. You can
write it two ways: inline in the class, or in a separate file.

## Inline templates

Return the markup from `inline_template()`, right in the component. No template file, no settings,
it works out of the box:

```interlace-demo InlineHello
# app/interlace/InlineHello.py
class InlineHello(Component):
    name: str = "Ada"

    def inline_template(self):
        return """
        <input lace:model.live="name">
        <p>Hello, <strong>{{ name }}</strong></p>
        """
```

Interlace compiles that string directly and **caches** the compiled template (the same way Django's
cached loader does for files), so it is parsed once and reused across requests. **You do not need to
add any template loader to your `settings.py`.**

## File templates

Or, if you prefer to keep the HTML in its own file (handy for larger markup), drop `inline_template()`
and let the convention find the file: a component with no `inline_template()` looks for
`interlace/<snake_case class name>.html` on your template path. A `UserProfile` component
automatically renders `interlace/user_profile.html`, nothing to wire up:

```python
# app/interlace/UserProfile.py
class UserProfile(Component):
    name: str = ""
```

```html
<!-- app/templates/interlace/user_profile.html -->
<div>
    <input lace:model.live="name">
    <p>Hello, {{ name }}</p>
</div>
```

The lookup is by **class name**, so `PascalCase` → `snake_case.html` (`StockTicker` →
`interlace/stock_ticker.html`), and it goes through Django's normal template resolution, any app's
`templates/interlace/` directory on the path is searched. If the component sits in a subfolder under
`interlace/`, the template mirrors it: `app/interlace/admin/UserRow.py` →
`interlace/admin/user_row.html`.

If you'd rather name the file yourself, set `template_name` explicitly and that wins over the
convention:

```python
class UserProfile(Component):
    template_name = "dashboard/profile_card.html"
```

## No wrapper element required

A component template does not need a single root element. Interlace renders several top-level
elements just fine, so you write exactly the markup you mean, with no wrapper `<div>` bolted on. This
very demo is a two-root component, a `<p>` and a `<button>` side by side, no enclosing element:

```interlace-demo RootlessCounter
# app/interlace/RootlessCounter.py
class RootlessCounter(Component):
    count: int = 0

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

    def inline_template(self):
        return """
        <p>Clicked <strong>{{ count }}</strong> times</p>
        <button lace:click="increment">Click me</button>
        """
```

This matters most for markup where a wrapper would be invalid or break layout, table rows, list
items, `<option>`s, or flex/grid children. **Laravel Livewire** and **Phoenix LiveView** both require
every component to have a single root element; Interlace does not.

Under the hood a multi-root component (a "fragment") is delimited by a pair of HTML comments rather
than an outer element. They are emitted as `<!--!lace:...-->` ("important" comments) so HTML minifiers
keep them by default; if your build step strips comments, allowlist the `lace:` prefix.

## Which to use

- **Inline** keeps a small component in one file, ideal for the common case.
- **A file template** suits larger markup, or when you use **django-cotton** tags (`<c-...>`), which
  are only expanded when a template is loaded from a file. See
  [Reusable components with Cotton](/docs/cotton-components/).
