Creating an Engine
Create a view engine withview.New():
Configuration Options
Dir
The directory where your templates are stored.FS
Anfs.FS filesystem to load templates from. Use this with embed.FS in production to bundle templates into your binary.
FS is set, templates are loaded from the embedded filesystem instead of the local filesystem. The directory structure inside the embedded FS should match what you’d have on disk.
Extension
The file extension for template files.DefaultLayout
The layout template to use when rendering pages. This refers to a file in thelayouts/ directory.
view.Layout() or disable it with view.NoLayout().
Delims
Custom template delimiters. Useful if your templates contain{{ and }} literally (common with Vue.js or Angular templates).
<% %> instead:
Development
Enables development mode features:- Template reload - Templates are re-read from disk on every request
- No caching - Templates are never cached
Funcs
Custom template functions to add to all templates.Built-in Functions
The view engine provides these functions by default:| Function | Description | Example |
|---|---|---|
dict | Create a map | {{dict "key" "value"}} |
list | Create a slice | {{list 1 2 3}} |
upper | Uppercase string | {{upper .Data.Name}} |
lower | Lowercase string | {{lower .Data.Name}} |
trim | Trim whitespace | {{trim .Data.Text}} |
contains | Check substring | {{if contains .Data.Text "hello"}} |
replace | Replace substring | {{replace .Data.Text "old" "new"}} |
split | Split string | {{split .Data.Tags ","}} |
join | Join slice | {{join .Data.Items ", "}} |
hasPrefix | Check prefix | {{if hasPrefix .Data.URL "https"}} |
hasSuffix | Check suffix | {{if hasSuffix .Data.File ".go"}} |
Engine Methods
New
Creates a new view engine with the given configuration.Load
Loads and validates all templates at startup. Call this in production to fail fast if templates have errors.layouts/ and pages/ directories.
Clear
Clears the template cache. Useful if you need to reload templates without restarting.Render
Renders a page template. The first argument is anio.Writer (like http.ResponseWriter), followed by the template name, data, and optional render options.
pages/ without the extension.
Middleware
Returns a Mizu middleware that adds the engine to every request context.view.From(c) and view.Render(c, ...) in handlers.
Package Functions
From
Retrieves the engine from a request context. Use this in handlers after adding the middleware.Render (package-level)
A convenience function that gets the engine from context and renders. This is the most common way to render templates.Render Options
When callingRender(), you can pass options to customize behavior.
Layout
Override the default layout for this render:layouts/admin.html instead of the default layout.
NoLayout
Render without any layout (useful for partial responses):Template Data Structure
When you render a template, your data is wrapped in a structure:Complete Example
Here’s a typical configuration for a production app:Error Handling
Template Errors
The engine returns anError type that wraps template parsing and execution errors:
Error type has:
Kind- “page”, “layout”, or “template”Name- Template nameErr- Underlying error (nil if not found)
ErrNotFound
Returned when a template file doesn’t exist:Best Practices
1. Use Development Mode Locally
Always enable development mode during development:2. Load Templates in Production
CallLoad() at startup to catch template errors early:
3. Embed Templates for Production
Useembed.FS so templates are bundled in your binary:
4. Keep Custom Functions Pure
Custom functions should be pure (no side effects):5. Use Consistent Data Structures
Create struct types for complex template data:view.Data (which is map[string]any), access it directly in templates: