Quickstart
In this quickstart you render templates/home.html with data that is checked
by the compiler. Every line below is the complete, real API.
Requirements
Section titled “Requirements”- Go 1.26 or newer (the version declared in templator’s
go.mod).
1. Install
Section titled “1. Install”go get github.com/alesr/templator@latestThe optional accessor generator is a separate binary, installed on demand in Generate typed accessors.
2. Create the template
Section titled “2. Create the template”templates/ home.html<h1>{{.Title}}</h1><p>{{.Content}}</p>The directory templates is the default location (constant
templator.DefaultTemplateDir). You can point the registry elsewhere with
WithTemplatesPath.
3. Define the data type
Section titled “3. Define the data type”This struct is the contract for every template in your registry:
type HomeData struct { Title string Content string}4. Render it
Section titled “4. Render it”package main
import ( "context" "log" "os"
"github.com/alesr/templator")
func main() { fs := os.DirFS(".")
reg, err := templator.NewRegistry[HomeData](fs) if err != nil { log.Fatal(err) // never returns an error today; see the API reference }
home, err := reg.Get("home") if err != nil { log.Fatal(err) // template file missing or unparsable }
err = home.Execute(context.Background(), os.Stdout, HomeData{ Title: "Welcome", Content: "Hello, World!", }) if err != nil { log.Fatal(err) }}Run it:
<h1>Welcome</h1><p>Hello, World!</p>Now try passing the wrong data type to Execute:
home.Execute(ctx, w, AboutData{Company: "acme"}) // does not compileThat is the whole idea: html/template would fail on this at runtime;
templator moves the failure to go build.
Where to go next
Section titled “Where to go next”- The type-safety model — what the compiler checks and what it does not.
- Registry and handlers — caching, concurrency, and context cancellation.
- Test templates — the full example with an in-memory filesystem.
- API reference — every type, option, and signature.