Templates
On this page 18
stx provides a powerful and flexible templating system that combines the elegance of Laravel Blade with modern TypeScript features. This guide covers everything you need to know about working with stx templates.
Basic Template Structure
A basic stx template looks like this:
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
const greeting = 'Welcome to stx!'
<h1>{{ greeting }}</h1>
<div></div>
</body>
</html>
Template Syntax
Variables and Expressions
Display variables using double curly braces:
<h1>{{ title }}</h1>
<p>{{ user.name }}</p>
For complex expressions:
<p>Total: {{ price * quantity }}</p>
<div>{{ isActive ? 'Active' : 'Inactive' }}</div>
TypeScript Integration
Use the @ts directive to write TypeScript code:
interface User {
name: string
email: string
isAdmin: boolean
}
const user: User = {
name: 'John',
email: 'john@example.com',
isAdmin: true
}
<div>
@if(userisAdmin)
<admin-panel />
</div>
Directives
stx provides several built-in directives for common tasks:
Conditionals
@if(userisAdmin)
<admin-panel />
@elseif(userisManager)
<manager-panel />
<user-panel />
@unless(userisBlocked)
<access-panel />
Loops
@foreach(usersasuser)
<user-card:user="user" />
@for(leti0i<5i++)
<div>Item {{ i + 1 }}</div>
@while(condition)
<div>Looped content</div>
Template Inheritance
stx supports template inheritance through layouts:
- Define a layout (
layouts/main.stx):
<!DOCTYPE html>
<html>
<head>
<title>@yield('')</title>
@section('')
</head>
<body>
<nav>
@include('')
</nav>
<main>
@section('')
</main>
<footer>
@include('')
</footer>
</body>
</html>
- Extend the layout in your template:
@extends('')
@section('', '')
@section('')
<metaname=""content="">
@section('')
<h1>Welcome</h1>
<p>Page content goes here</p>
Including Partials
Break your templates into reusable partials:
@include('')
<main>
@include('' '' })
@include('')
</main>
@include('')
Components
Use components for reusable UI elements:
@component('' '' '' })
<h2>{{ title }}</h2>
<slot></slot>
@slot('')
<button>Read More</button>
Error Handling
Handle errors gracefully in templates:
{{ potentiallyErroringFunction() }}
(error)
<error-display:message="error" />
Best Practices
- Keep Templates Focused: Each template should have a single responsibility
- Use Components: Break complex UIs into reusable components
- Leverage TypeScript: Use TypeScript for type safety and better IDE support
- Consistent Naming: Follow a consistent naming convention for templates and partials
- Comments: Use comments to explain complex logic or important notes
{{-- This is a template comment --}}
// This is a TypeScript comment
Performance Tips
- Use
@oncefor content that doesn't need to be re-rendered:
<heavy-component />
- Leverage caching when possible:
('key')
<expensive-component />
- Use lazy loading for heavy components:
<heavy-feature />
Advanced Features
Custom Directives
Create custom directives for specialized functionality:
('uppercase')
return value.toUpperCase()
<h1>('hello')</h1> <!-- Outputs: HELLO -->
State Management
Integrate with state management:
import { useStore } from '@/store'
const store = useStore()
<div>
<h1>{{ store.user.name }}</h1>
<button@click="store">Logout</button>
</div>
Streaming Support
stx supports streaming for improved performance:
<real-time-feed />
Debug Mode
Enable debug mode during development:
{{ someVariable }}
This will output additional debugging information in the browser console.