A Go template theme management library that provides a flexible system for organizing and rendering HTML templates with support for template inheritance, themes, and parent-child relationships.
go get github.com/gowool/gotpackage main
import (
"errors"
"log"
"net/http"
"os"
"github.com/gowool/got"
)
func main() {
// Create store from filesystem
store := got.NewStoreFS(os.DirFS("themes"))
// Create theme
theme := got.NewTheme("default", store)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
// Render template
if err := theme.Write(r.Context(), w, "page/index.gohtml", map[string]any{
"Title": "Hello World",
"Content": "Welcome to GO Theme",
}); err != nil {
log.Println(err.Error())
}
})
if err := http.ListenAndServe(":8080", nil); err != nil && !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}Templates use HTML comments to define inheritance paths:
<!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
</head>
<body>
{{block "content" .}}Default content{{end}}
</body>
</html><!-- layouts/base.gohtml -->
{{define "content"}}
<h1>{{.Title}}</h1>
<p>{{.Content}}</p>
{{end}}Create parent-child theme relationships:
parent := got.NewTheme("default", store)
child := got.NewTheme("custom", store)
child.SetParent(parent)
// Child theme will fallback to parent for missing templatesstore := got.NewStoreFS(os.DirFS("themes"))store := got.NewStoreMemory()
store.SetTemplate("theme", "template.html", "content")chain := got.NewStoreChain()
chain.Add(memoryStore)
chain.Add(fsStore)This project is licensed under the MIT License - see the LICENSE file for details.