What is a Layout?
A layout is a template that defines the outer structure of your pages. Instead of repeating the HTML boilerplate in every page, you define it once in a layout. Without layouts - every page repeats this:Directory Structure
Layouts live in thelayouts/ subdirectory:
Creating a Layout
Basic Layout
{{.Content}} is where the rendered page content is inserted.
Page Template
Pages simply provide content - no layout wrapper needed:How It Works
When you render a page:- Renders
pages/home.htmlwith your data - Puts the result in
.Content - Renders
layouts/default.htmlwith.Contentset
Setting the Default Layout
Configure the default layout in your engine:Multiple Layouts
Create different layouts for different sections of your site.Admin Layout
Using a Different Layout
Override the default layout when rendering:Minimal Layout
For simple pages that donβt need full navigation:No Layout
For AJAX responses, partial HTML, or API endpoints that return HTML fragments:- HTMX partial updates
- AJAX content loading
- Email templates (that define their own complete HTML)
Layout Data Access
Layouts have access to all the same data as pages:| Field | Description |
|---|---|
.Data | Your data from the handler |
.Page.Name | Page template name |
.Page.Layout | Layout name |
.Content | Rendered page content |
Common Patterns
Dynamic Navigation
Highlight the current page:Flash Messages
Display flash messages from sessions:Page-Specific Body Classes
Meta Tags
Conditional Scripts
Best Practices
1. Keep Layouts Focused
Each layout should serve a clear purpose:default.html- Main public pagesadmin.html- Admin panelauth.html- Login/signup pagesemail.html- Email templates
2. Pass Common Data via Middleware
Instead of passing the same data in every handler, use middleware:3. Use Meaningful Names
4. Donβt Overcomplicate
Start simple. Most sites only need 2-3 layouts:- Main layout for public pages
- Admin layout for backend
- Minimal layout for auth pages