# Sprig
Reactive Twig component framework by PutYourLightsOn. Lets you create components that re-render themselves on user-triggered events (clicks, input changes, form submissions) via AJAX — no JavaScript required. Built on htmx. Enables live search, load more, pagination, filtering, tabs, and form submissions without page refreshes. 13K+ installs.
`putyourlightson/craft-sprig` — Free (MIT)
## Documentation
- Full docs: https://putyourlightson.com/plugins/sprig
- Cookbook: https://putyourlightson.com/sprig/cookbook
- GitHub: https://github.com/putyourlightson/craft-sprig
When unsure about a Sprig attribute or function, `WebFetch` the docs page. The cookbook has ready-to-use patterns for common scenarios.
## Core Concept
A Sprig component is a Twig template that:
1. Lives in `templates/_components/` (convention, not requirement)
2. Is rendered with `{{ sprig('_components/my-component') }}`
3. Has a single root element
4. Re-renders itself via AJAX when elements with the `sprig` attribute are triggered
The `sprig` attribute on an element makes it "reactive" — on its trigger event (click by default, change for inputs), it sends an AJAX request that re-renders the component with updated variables.
## Common Pitfalls
- **Multiple root elements** — a Sprig component must have a single root HTML element. Everything must be wrapped in one container.
- **Not initializing variables with defaults** — every variable used in the component must have a default: `{% set query = query ?? '' %}`. Without this, the first render fails because the variable doesn't exist yet.
- **Using `{% js %}` / `{% css %}` / `{% html %}` tags** — these don't re-render during Sprig AJAX requests. Use `sprig.registerJs()` instead for JavaScript.
- **Variables named like Twig globals** — Sprig disallows variables named the same as Twig globals (e.g., `craft`, `currentSite`). Use unique names.
- **Missing `s-target` and `s-swap`** — without these, the entire component re-renders. Be explicit about what gets swapped for performance.
- **Not using `s-trigger` with delay on inputs** — without `delay:300ms`, every keystroke fires a request. Always debounce text inputs.
- **Expecting full-page Twig context** — Sprig components render in isolation. They don't have access to entry variables from the parent page. Pass what you need as initial variables.
- **Caching Sprig components** — Sprig components should not be statically cached (Blitz). Use `{% cache %}` sparingly and exclude Sprig component markup. Use `{{ sprig.htmxScript }}` outside cached blocks.
## Creating a Component
### Include in a Template
```twig
{# In your page template #}
{{ sprig('_components/search') }}
{# With initial variables #}
{{ sprig('_components/load-more', { limit: 5, section: 'news' }) }}
{# With HTML attributes (third parameter) #}
{{ sprig('_components/search', {}, {
'id': 'main-search',
'class': 'search-widget',
's-trigger': 'load, refresh',
}) }}
```
### Component Template
```twig
{#--- _components/search ---#}
{# Always set defaults — these variables won't exist on first render #}
{% set query = query ?? '' %}
{% if query %}
{% set entries = craft.entries.search(query).limit(10).all() %}
{% for entry in entries %}
{{ entry.title }}
{% endfor %}
{% if not entries|length %}
No results found.
{% endif %}
{% endif %}
```
## Sprig Attributes (`s-*`)
All attributes are prefixed with `s-` (Sprig's namespace over htmx's `hx-`).
| Attribute | Description | Example |
|-----------|-------------|---------|
| `sprig` | Makes element reactive (required on trigger elements) | `