Directives
On this page 30
stx directives are special template attributes that provide powerful functionality for your templates. This guide covers all available directives and their usage.
Core Directives
Conditional Rendering
@if,v-if, and:ifare the same conditional in different syntax (v-ifis sugar for@if;x-iffor:if). What makes a conditional reactive is the data it reads, not the keyword: a condition that reads a signal re-evaluates on change (including@if/@elsechains on a signals page); a condition over server data renders once. See Conditionals for the full breakdown.
@if / @else / @elseif
@if(userisAdmin)
<admin-panel />
@elseif(userisModerator)
<moderator-panel />
<user-panel />
@unless
The opposite of @if:
@unless(userisVerified)
<verify-email-banner />
@show / @hide
Shorthand for visibility control:
<div@show="isVisible">Visible content</div>
<div@hide="isHidden">Hidden when true</div>
Loops and Iteration
@foreach
Iterate over arrays and objects:
<ul>
@foreach(usersasuser)
<li>{{ user.name }}</li>
</ul>
<!-- With index -->
<ul>
@foreach(usersasuserindex)
<li>{{ index + 1 }}. {{ user.name }}</li>
</ul>
<!-- With empty state -->
@foreach(itemsasitem)
<item-card:data="item" />
<p>No items found</p>
@for
Traditional for loop:
@for(leti0i<5i++)
<span>Item {{ i + 1 }}</span>
@while
While loop:
@while(condition)
<keep-trying />
Layout Control
@extends
Inherit from a layout:
@extends('')
@section / @yield
Define and render content sections:
<!-- Layout -->
<title>@yield('')</title>
<main>@yield('')</main>
<!-- Page -->
@section('', '')
@section('')
<h1>Welcome</h1>
@include
Include other templates:
@include('')
@include('' '' })
@includeIf('')
@includeWhen(userisAdmin, '')
Component Directives
@component
Define a component:
interface ButtonProps {
type: 'primary' | 'secondary'
size?: 'sm' | 'md' | 'lg'
}
@component(''
props: {
type: 'primary',
size: 'md'
}
})
<buttonclass="{{type}}{{size}}">
<slot></slot>
</button>
@slot
Define named slots:
@component('')
<slotname=""></slot>
<slot></slot>
<slotname=""></slot>
<!-- Usage -->
<Card>
@slot('')
<h2>Card Title</h2>
<p>Card content</p>
@slot('')
<button>Action</button>
</Card>
Event Handling
@click
Handle click events:
<button@click="handleClick">Click Me</button>
<button@click="count++">Increment</button>
@submit
Handle form submissions:
<form@submit.prevent="handleSubmit">
<!-- form fields -->
</form>
Event Modifiers
Add event modifiers with dots:
<button@click.stop="handleClick">Stop Propagation</button>
<button@click.prevent="handleClick">Prevent Default</button>
<form@submit.prevent="handleSubmit">Submit</form>
<div@click.once="handleClick">Click Once Only</div>
<button@click.self="handleClick">Self Only</button>
Data Binding
@bind
Two-way data binding:
<input@bind="username" />
<select@bind="selectedOption">
<optionvalue="">One</option>
<optionvalue="">Two</option>
</select>
@model
Form input binding:
<inputtype=""@model="user" />
<inputtype=""@model="isChecked" />
<select@model="selected">
<!-- options -->
</select>
Advanced Directives
@memo
Memoize expensive computations:
(expensiveValue)
<heavy-componentx-value="" />
@watch
Watch for changes:
(user.status)
<status-indicator:status="user" />
@error
Error handling:
<divclass="">
{{ error.message }}
</div>
Custom Directives
Create your own directives:
// directives/tooltip.ts
import { defineDirective } from '@stacksjs/stx'
export const tooltip = defineDirective({
mounted(el, { value }) {
el.setAttribute('title', value)
},
updated(el, { value }) {
el.setAttribute('title', value)
}
})
// Usage in template
<button @tooltip="Help text">Hover me</button>
Best Practices
-
Conditional Rendering
- Use
@iffor simple conditions - Use
@switchfor multiple conditions - Consider extracting complex logic to computed properties
- Use
-
Loops
- Always provide a key for list items
- Use
@emptyfor empty states - Consider pagination for large lists
-
Event Handling
- Use appropriate event modifiers
- Keep event handlers simple
- Consider debouncing/throttling when needed
-
Component Organization
- Keep directives focused and reusable
- Document custom directives
- Test directive behavior
Next Steps
- Learn about Components
- Explore Template Syntax
- Understand TypeScript Integration
- Check out Custom Directives