Template Basics
Mizu uses Goβs standardhtml/template package. Templates are HTML files with special markers ({{ and }}) for dynamic content.
Accessing Data
In Mizu templates, your data is wrapped in a structure. Access your data using.Data:
The Template Data Structure
Templates receive this structure:Go Template Syntax
Outputting Values
Use.Data. to access your data:
Conditions
Useif to show content conditionally:
Comparison Operators
Go templates provide comparison functions:Boolean Logic
Combine conditions withand, or, and not:
Loops
Userange to iterate over slices, arrays, or maps:
Looping Over Structs
When ranging over a slice of structs:Looping Over Maps
Variables
Assign values to variables with:=:
The Root Context ($)
Inside arange loop, . refers to the current item. Use $ to access the root context:
$ always refers to the original data passed to the template.
Pipelines
Chain operations with|:
Comments
Template comments are stripped from output:Whitespace Control
Use- to trim whitespace:
Layouts and Pages
Layout Structure
Layouts use{{.Content}} to include the rendered page:
Page Structure
Pages are simple - just write HTML with template expressions:{{.Content}}.
Common Patterns
Conditional Classes
Dynamic Attributes
Safe HTML Output
By default, Go templates escape HTML to prevent XSS attacks:Empty/Nil Checks
Building Links
Formatting Numbers and Dates
Use custom functions orprintf:
Debugging Templates
Print Values
Useprintf with %#v to see the Go syntax representation:
Check Types
Enable Development Mode
For detailed error messages with line numbers:Best Practices
1. Keep Templates Simple
Put complex logic in Go handlers, not templates:2. Use Meaningful Variable Names
3. Document Expected Data
Add comments at the top of templates:4. Escape User Input
Always let Goβs default escaping protect against XSS. Only usetemplate.HTML for content you trust completely.