An opinionated HTML template renderer for Golang.
go get github.com/nicolasparada/go-tmpl-renderer
templates/includes/layout.tmpl
templates/includes/header.tmpl
templates/welcome.tmpl
main.go
package main
import (
// other imports
tmplrenderer "github.com/nicolasparada/go-tmpl-renderer"
)
//go:embed templates/includes/*.tmpl templates/*.tmpl
var templatesFS embed.FS
func main() {
renderer := &tmplrenderer.Renderer{
FS: templatesFS,
BaseDir: "templates",
IncludePatters: []string{"includes/*.tmpl"},
}
http.HandleFunc("/welcome", func(w http.ResponseWriter, r *http.Request) {
renderer.Render(w, "welcome.tmpl", nil)
})
http.ListenAndServe(":4000")
}
HTML templates will include two functions from the start: dict
and list
.
dict
given a list of key-value pairs, it will construct a mapmap[string]any
out of it.
Example:
{{$_ := dict "foo" "bar"}} => map[string]any{"foo": "bar"}
It's very usefull for passing named parameters to other templates, for example:
list
: given a list of items, it will construct an slice[]any
out of it.
Example:
{{$_ := list "foo" "bar"}} => []any{"foo", "bar"}