Web & UI/Crest Templates
Crest Templates
HTML-first templates compiled to Go: expressions, control flow, slots, layouts, dynamic <f:icon>, and Alpine attributes.
On this page
Crest is Fletch’s HTML-first template language. Templates live in .crest files and compile via fletch gen into ordinary *_crest.go files with a Render(ctx context.Context, w io.Writer) error method. There is no runtime template parser or reflection in production.
Component Structs and the Receiver
Each .crest file binds to a Go struct in the same package. By default the struct is named after the file (board.crest binds Board, task_row.crest binds TaskRow), and the receiver is the lower-case first letter of the type (b for Board), or the receiver name of a method you already declared on the type. Because generated code is standard Go, expressions are type-checked by the Go compiler.
// modules/tasks/views/board.go
package views
type Task struct {
ID string
Title string
Done bool
}
type Board struct {
Heading string
Tasks []Task
}
<!-- modules/tasks/views/board.crest -->
<section>
<h1>{b.Heading}</h1>
{if len(b.Tasks) == 0}
<p>No tasks yet.</p>
{else}
<ul>
{for _, t := range b.Tasks key t.ID}
<li class:done={t.Done}>{t.Title}</li>
{/for}
</ul>
{/if}
</section>
Running fletch gen emits board_crest.go defining func (b Board) Render(ctx context.Context, w io.Writer) error.
Two optional directives at the very top of the file override the defaults, each on its own line: {type Name} binds a different struct, and {receiver x} picks a different receiver name.
{type Board}
{receiver board}
<h1>{board.Heading}</h1>
Expressions and Control Flow
Expressions inside {expr} are contextually HTML-escaped before writing to w. Crest has two control-flow blocks, {if} and {for}:
{if u.IsAdmin}
<span class="badge">Admin</span>
{else if u.IsMember}
<span class="badge">Member</span>
{else}
<span class="badge">Guest</span>
{/if}
{for _, item := range l.Items key item.ID}
<div>{item.Name}</div>
{/for}
{if} takes a plain Go boolean expression; an initializer statement such as {if msg := f(); msg != ""} is rejected. {for} takes a Go range clause, followed by key <expr>.
Include a key on {for} loops. Omitting it produces warning W0310: the template still compiles, but Live then diffs rows by position, so inserting or removing a row can disturb DOM state in the rows after it.
Layouts and Slots
Application layouts live in ui/layouts/. The scaffolded ui/layouts/app.crest binds the App struct, whose Head, Scripts, and Children fields (all crest.Component) fill the named and default slots:
<!-- ui/layouts/app.crest -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{a.Title}</title>
<link rel="stylesheet" href={asset("css/app.css")} />
<slot name="head" />
</head>
<body>
<main><slot /></main>
<script type="module" src={asset("js/app.js")}></script>
<slot name="scripts" />
</body>
</html>
Views compose the layout as a component element and supply named slots alongside the default body. The view’s Go file needs a blank import of the layout package (_ "myapp/ui/layouts") so layouts.App resolves:
<layouts.App title="Tasks">
<slot:head>
<meta name="description" content="Team task board" />
</slot:head>
<h1>Tasks</h1>
<p>Rendered into the default slot.</p>
<slot:scripts>
<script src={asset("js/board.js")} defer></script>
</slot:scripts>
</layouts.App>
Built-In Elements
Built-in <f:*> elements compile through the same public plugin API available to custom elements.
| Element | Purpose |
|---|---|
<f:csrf /> |
Emits the hidden CSRF token input for HTML forms. |
<f:img> |
Renders responsive <picture> markup backed by github.com/fletchhq/fletch/images. |
<f:markdown> |
Renders sanitized Markdown content. |
<f:alpine /> |
Emits nonce’d $fletch bridge and Alpine.js <script> tags inside <head>. |
<f:icon> |
Inlines SVG icons resolved through elements/icon/iconset. |
Dynamic Props on <f:icon>
<f:icon> resolves static icon names from elements/icon/iconset at compile time while supporting dynamic Go expressions for size and class:
<f:icon name="check" size={sizeExpr} class={classExpr} />
Register custom SVG packs or icon lookups through elements/icon/iconset so project-specific icon names resolve during fletch gen.
Alpine.js Attribute Pass-Through
Quoted Alpine.js attributes pass through Crest untouched as static HTML attributes. Curly braces inside quoted Alpine expressions do not collide with Crest’s {expr} syntax, and directives such as x-data="{ open: false }", :class="{ 'is-open': open }", @click.outside="open = false", x-on:keydown.escape.window="open = false", and x-transition:enter="..." work directly alongside Crest class:, on:, and bind: directives.
<div
x-data="{ open: false }"
@click.outside="open = false"
x-on:keydown.escape.window="open = false"
>
<button type="button" @click="open = !open" :class="{ 'is-active': open }">
Options
</button>
<div x-show="open" x-transition:enter="fade-in">
<slot />
</div>
</div>
Crest blocks unquoted Go expressions in Alpine and inline JS handler attributes (x-data={...}, @click={...}, :class={...}, and any x-* attribute except x-transition* and x-ref) with diagnostic W0313. This prevents untrusted server strings from turning into executable JavaScript. Pass dynamic server values via data-* attributes or $fletch.signal instead. See Alpine.js & Live Interactivity for details.
Compiler Diagnostics and Common Gotchas
fletch gen and fletch check report template and directive diagnostics across codes W0300 through W0315 and W0330 through W0344, plus asset checks such as W0401.
| Code | Cause | Resolution |
|---|---|---|
W0309 |
Invalid bind:value or bind:checked signal binding |
Bind bind:value and bind:checked to a valid Live signal on a compatible form element. |
W0310 |
Warning: a {for} loop has no key |
Add a stable unique key: {for _, x := range xs key x.ID}. |
W0313 |
Dynamic Go expression in an Alpine or JS handler attribute | Move server values into data-* attributes or $fletch.signal (only x-transition* and x-ref permit dynamic Go values). |
W0314 |
Live form input matches PCI payment card field naming | Collect card data in your payment provider’s hosted iframe or element so card numbers never reach your server or Live state. |
W0315 |
alpine.Standard() enabled without 'unsafe-eval' in secure.Config |
Stay on the default @alpinejs/csp build, or explicitly allow 'unsafe-eval' in secure.Config. |
W0401 |
Unresolved asset("...") reference |
Verify the asset path exists under resources/ and matches the build manifest. |
To build your own <f:*> compiler elements using the same public plugin interface, see Crest Plugins. For pairing Crest templates with client-side Alpine state and Live updates, see Alpine.js & Live Interactivity.