--- title: STX View Templates description: Build server-rendered and reactive interfaces with STX templates in Stacks. --- # Views Stacks uses STX for views and components. STX combines server rendering, reactive client code, component composition, and scoped styles in `.stx` files. Application views live in `resources/views/`. Reusable components, layouts, and partials live in their matching directories under `resources/`. ## Create a view Create `resources/views/welcome.stx`: ```html ``` Browser auto-imports make `ref`, `computed`, `watch`, and the supported composables available inside STX scripts. Do not use direct `document` or `window` access. Keep client behavior in signals, composables, directives, and component methods. ## Rendering data Double braces escape interpolated values: ```html

{{ user.name }}

{{ items.length }} items

``` Use the raw-output form only for trusted HTML: ```html
{!! trustedHtml !!}
``` Server scripts run before the template is rendered and can prepare data for the page: ```html ``` ## Conditionals and loops Use Blade-style directives for server-rendered control flow: ```html @if(user)

Welcome back, {{ user.name }}.

@else Sign in @endif ``` ```html @foreach(posts as post)

{{ post.title }}

@endforeach ``` Use `@for` and `@while` when an indexed loop or condition is a better fit: ```html @for(let index = 0; index < featured.length; index++)

{{ index + 1 }}. {{ featured[index].name }}

@endfor ``` ## Components Components live in `resources/components/` and are auto-resolved by name. A component at `resources/components/UserCard.stx` can be used directly: ```html ``` Declare typed props inside the component: ```html ``` ## Events and state Bind component methods with event directives: ```html ``` For shared behavior, place a function or composable in `resources/functions/`. Browser auto-import generation makes exported functions available to templates after `buddy generate`. ## Slots Slots let a component accept caller-provided content: ```html ``` ```html

Manage your profile and security settings.

``` ## Layouts and partials Layouts live in `resources/layouts/`. Select one with frontmatter or the layout directive supported by your application configuration. Partials live in `resources/partials/` and can be included from a view: ```html @include('partials/header')
{{ content }}
@include('partials/footer') ``` Pass values to a partial when it needs local context: ```html @include('partials/account-menu', { user }) ``` ## Styling Use ts-css utilities in templates. Component styles can be scoped when custom CSS is necessary: ```html ``` Prefer ts-css transitions and CSS keyframes for motion. Use `usePreferredReducedMotion()` before enabling non-essential animation. ## Configuration STX configuration lives in `config/ui.ts`: ```ts import type { StxOptions } from '@stacksjs/stx' export default { componentsDir: 'components', layoutsDir: 'layouts', partialsDir: 'partials', } satisfies StxOptions ``` The `bun-plugin-stx` plugin compiles `.stx` files during development and production builds. ## Commands ```bash buddy make:view account buddy make:component UserCard buddy dev buddy build buddy generate ``` Use `buddy make:page` as an alias for `buddy make:view`. Run `buddy generate` after adding browser functions or components that should appear in generated auto-import types. ## Related guides - [Components](/basics/components) - [Functions](/basics/functions) - [Routing](/basics/routing) - [STX package reference](/packages/stx)