Categorygithub.com/hostwithquantum/tpl
repositorypackage
0.0.0-20250219125047-57331987c124
Repository: https://github.com/hostwithquantum/tpl.git
Documentation: pkg.go.dev

# README

tpl

A small wrapper around html/template. This is not optimized for performance, but optimized for using this library to write somewhat decent code.

Example

Put your files into a templates/ directory.

The only required file is a layout.gotmpl with appropriate blocks for the layout and content.

{{ define "layout" }}
<!DOCTYPE html>
<html lang="en">
<body>
  <main class="container">
    {{ template "content" . }}
  </main>
</body>
</html>
{{ end }}

Initialize:

var (
	//go:embed templates/*
	tmplFS embed.FS

)
renderer := tpl.NewRender(tmplFS, "name")

... and pass it into your router/handlers.

Define a struct like this and assign data to it so your templates have something to show.

type tplData struct {
	Data any
} 

Output a template from your handler:

// filename.gotmpl
tpl.Render(w, "filename", tplData{
	Data: allData,
})