liveBook · Manning

Html/template package

As explained in Learn Go with Pocket-Sized Projects

html/template is Go's standard-library package for parsing and executing HTML templates with contextual escaping of HTML-sensitive content.

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

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Works through parsing, execution, actions, embedded files and reusable templates; does not explain escaping internals.
Examples
Uses a habit-count page, an extracted habit list item, a directory of embedded templates and a contrasting CSS template.
What it assumes
Assumes familiarity with Go handlers and an existing habits service while introducing template syntax from a single printed value.
What you can do after
Build it from scratch: parse and execute an embedded HTML page, then split repeated markup into a named template.
How it connects
Connects HTML escaping with text/template, HTTP handlers, Go embedding, template actions and gRPC-backed data.

Also explained in

Compare how 2 books explain html/template package →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.