---
title: Build a Frontend
description: "This tutorial will guide you through building a frontend application with Stacks. You will learn how to create components using STX (Stacks Template Syntax..."
---
# Build a Frontend
This tutorial will guide you through building a frontend application with Stacks. You will learn how to create components using STX (Stacks Template Syntax), style them with Headwind CSS utilities, manage state, and set up routing.
## Stacks UI Engine
The Stacks UI engine is responsible for rendering the UI of your application. It is designed to be framework-agnostic, meaning that the built results can be used with any JavaScript or TypeScript framework.
The UI engine handles two core responsibilities:
1. **Rendering the Application** - Your main web application
2. **Rendering the Documentation** - Your STX docs site powered by BunPress
## Creating Components with STX
STX (Stacks Template Syntax) is a simple, HTML-like syntax for building components. Components live in the `resources/components/` directory.
### Basic Component Structure
An STX component consists of three parts: script, template, and optional styles.
```html
{{ greeting }}
@if(showTime)
Current time: {{ currentTime }}
@endif
```
### Using Props
Props allow you to pass data from parent to child components:
```html
@if(!href)
@else
@endif
```
Using the component:
```html
```
### Using Slots
Slots allow you to pass content into components:
```html
@if(title)
{{ title }}
@endif
@if(slots.footer)
@endif
```
Using named slots:
```html
This is the main content.
```
### Conditionals and Loops
STX provides Blade-like directives for control flow:
```html
@if(items.length > 0)
@for(item of items)
{{ item.name }}
@if(item.isNew)
New
@endif
@endfor
@elseif(showEmpty)
No items found
@endif
```
## Styling with Headwind
Stacks uses Headwind, a utility-first CSS framework similar to Tailwind CSS. You can use utility classes directly in your templates.
### Common Utility Classes
```html
Title
Body text
Colored text
```
### Using Icons
Stacks supports icon classes using the `i-` prefix:
```html
```
## State Management
### Local Component State
For simple state within a component, use the script section:
```html
{{ count }}
```
### Reactive State
For reactive state that updates the UI:
```html
Count: {{ count() }}
Doubled: {{ doubled() }}
```
Signals are callable: `count()` reads, `count.update()` writes. There is no
`.value`, and reactive state belongs in `
Current theme: {{ theme() }}
```
## Routing
Stacks supports two routing strategies for your frontend:
### File-based Routing (Default)
With file-based routing, your file structure determines your routes. Files in `resources/views/` automatically become routes.
```
resources/views/
├── index.stx # /
├── about.stx # /about
├── contact.stx # /contact
├── blog/
│ ├── index.stx # /blog
│ └── [slug].stx # /blog/:slug (dynamic route)
└── dashboard/
├── index.stx # /dashboard
└── settings.stx # /dashboard/settings
```
#### Dynamic Routes
Use square brackets for dynamic segments:
```html
Blog Post: {{ slug }}
```
### Laravel-like Routing
For more control, use explicit route definitions:
```typescript
// routes/web.ts
import { route } from '@stacksjs/router'
route.get('/', 'HomeController@index')
route.get('/about', 'AboutController@show')
route.get('/blog/:slug', 'BlogController@show')
route.get('/dashboard', 'DashboardController@index').middleware('auth')
```
### Navigation
Use the Link component or standard anchor tags:
```html
```
## Layouts
Create reusable layouts for consistent page structure:
```html
{{ title }}
```
Using the layout in a view:
```html
Welcome to My App
Build amazing things with Stacks.
```
## Complete Example: Task Manager
Let us build a simple task manager to demonstrate all concepts:
```html
{{ task.title }}
```
```html
Task Manager
@for(task of tasks())
@endfor
{{ completedCount() }} of {{ tasks().length }} tasks completed
```
## Configuration
Configure your STX components in `stx.config.ts`:
```typescript
// stx.config.ts
export default {
// Components directory - for user-defined components
componentsDir: 'components',
// Layouts directory - for layout templates
layoutsDir: 'layouts',
// Partials directory - for partial templates
partialsDir: 'partials',
}
```
## Next Steps
Now that you know how to build frontends with Stacks, continue to:
- [Build an API](/bootcamp/api) - Create backend endpoints for your frontend
- [Authentication How-To](/bootcamp/how-to/authentication) - Add user authentication
- [Testing How-To](/bootcamp/how-to/testing) - Write tests for your components
## Related Documentation
- [Components Guide](/basics/components)
- [Views Guide](/basics/views)
- [Routing Guide](/basics/routing)