Skip to content

Templates ​

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:

html
<!DOCTYPE html>
<html>
<head>
  <title>{{ title }}</title>
</head>
<body>
  @ts
  const greeting = 'Welcome to STX!'
  @endts

  <h1>{{ greeting }}</h1>
  <div>@slot</div>
</body>
</html>

Template Syntax ​

Variables and Expressions ​

Display variables using double curly braces:

html
<h1>{{ title }}</h1>
<p>{{ user.name }}</p>

For complex expressions:

html
<p>Total: {{ price * quantity }}</p>
<div>{{ isActive ? 'Active' : 'Inactive' }}</div>

TypeScript Integration ​

Use the @ts directive to write TypeScript code:

html
@ts
interface User {
  name: string
  email: string
  isAdmin: boolean
}

const user: User = {
  name: 'John',
  email: 'john@example.com',
  isAdmin: true
}
@endts

<div>
  @if(user.isAdmin)
    <admin-panel />
  @endif
</div>

Directives ​

STX provides several built-in directives for common tasks:

Conditionals ​

html
@if(user.isAdmin)
  <admin-panel />
@elseif(user.isManager)
  <manager-panel />
@else
  <user-panel />
@endif

@unless(user.isBlocked)
  <access-panel />
@endunless

Loops ​

html
@foreach(users as user)
  <user-card :user="user" />
@endforeach

@for(let i = 0; i < 5; i++)
  <div>Item {{ i + 1 }}</div>
@endfor

@while(condition)
  <div>Looped content</div>
@endwhile

Template Inheritance ​

STX supports template inheritance through layouts:

  1. Define a layout (layouts/main.stx):
html
<!DOCTYPE html>
<html>
<head>
  <title>@yield('title')</title>
  @section('meta')
  @endsection
</head>
<body>
  <nav>
    @include('partials/nav')
  </nav>

  <main>
    @section('content')
    @endsection
  </main>

  <footer>
    @include('partials/footer')
  </footer>
</body>
</html>
  1. Extend the layout in your template:
html
@extends('layouts/main')

@section('title', 'My Page')

@section('meta')
  <meta name="description" content="Page description">
@endsection

@section('content')
  <h1>Welcome</h1>
  <p>Page content goes here</p>
@endsection

Including Partials ​

Break your templates into reusable partials:

html
@include('partials/header')

<main>
  @include('partials/sidebar', { active: 'home' })
  @include('partials/content')
</main>

@include('partials/footer')

Components ​

Use components for reusable UI elements:

html
@component('Card', { title: 'Welcome', theme: 'dark' })
  <h2>{{ title }}</h2>
  <slot></slot>
  
  @slot('footer')
    <button>Read More</button>
  @endslot
@endcomponent

Error Handling ​

Handle errors gracefully in templates:

html
@try
  {{ potentiallyErroringFunction() }}
@catch(error)
  <error-display :message="error.message" />
@endtry

Best Practices ​

  1. Keep Templates Focused: Each template should have a single responsibility
  2. Use Components: Break complex UIs into reusable components
  3. Leverage TypeScript: Use TypeScript for type safety and better IDE support
  4. Consistent Naming: Follow a consistent naming convention for templates and partials
  5. Comments: Use comments to explain complex logic or important notes
html
{{-- This is a template comment --}}
@ts
// This is a TypeScript comment
@endts

Performance Tips ​

  1. Use @once for content that doesn't need to be re-rendered:
html
@once
  <heavy-component />
@endonce
  1. Leverage caching when possible:
html
@cache('key')
  <expensive-component />
@endcache
  1. Use lazy loading for heavy components:
html
@lazy
  <heavy-feature />
@endlazy

Advanced Features ​

Custom Directives ​

Create custom directives for specialized functionality:

html
@directive('uppercase')
  return value.toUpperCase()
@enddirective

<h1>@uppercase('hello')</h1> <!-- Outputs: HELLO -->

State Management ​

Integrate with state management:

html
@ts
import { useStore } from '@/store'
const store = useStore()
@endts

<div>
  <h1>{{ store.user.name }}</h1>
  <button @click="store.logout()">Logout</button>
</div>

Streaming Support ​

STX supports streaming for improved performance:

html
@stream
  <real-time-feed />
@endstream

Debug Mode ​

Enable debug mode during development:

html
@debug
  {{ someVariable }}
@enddebug

This will output additional debugging information in the browser console.

Released under the MIT License.