Use `html/template` when an HTTP handler must combine HTML markup with changing data. It is especially useful when a page has enough static structure that assembling strings with `fmt.Sprintf` or `strings.Builder` becomes cumbersome. Unlike `text/template`, it escapes HTML-sensitive values inserted into the output, helping prevent data from being interpreted as injected markup or code. section 11.1.2 section 11.6
Render a value
Put `<p>You have {{.}} habits to follow.</p>` in `index.html`. The action `{{.}}` prints the current data value, called the dot. In a handler, parse the page with `template.New("index").Parse(indexPage)`, check the error, then call `tpl.Execute(w, 5)` and check that error too. The resulting HTML reports five habits. section 11.1.2 section 11.6
Keep substantial templates in separate files and embed them with `//go:embed`. Template actions can read exported fields, iterate with `range`, and select output with `if`; the template must therefore use field names that the Go data actually exposes. Whitespace-trimming forms such as `{{- ... }}` are useful for controlling generated HTML, but can also remove indentation. section 11.5.3 section 11.6
Reuse a habit item
Extract a repeated list item into `habititem.html` with `{{ define "habitItem" }}...{{ end }}`. Render each habit from the page with `{{ range .Habits }}{{ template "habitItem" . }}{{ end }}`. Embed the directory with `//go:embed templates/*.html`, register any required Go functions through `Funcs(template.FuncMap{...})`, and parse with `ParseFS`. When the parsed set has no body for the name passed to `New`, `Execute` can report an incomplete or empty template; call `ExecuteTemplate(w, "index.html", data)` to select the page explicitly. section 11.5.3
Choose the template package for the output format rather than using HTML escaping everywhere. A CSS response can instead use `text/template`, set `Content-Type` to `text/css`, and execute with color values. For structured formats or untrusted input, consider the consequences of a bad value before choosing templating. section 11.5.3 section 11.1.2

