liveBook · Manning

Template engine

As explained in Learn Go with Pocket-Sized Projects

A template engine is software that processes templates by evaluating embedded actions and substituting resulting values into output text.

A template engine binds a template to a data model, parses the template, and executes its actions to produce output. Ordinary text is copied unchanged; actions enclosed in {{...}} evaluate values supplied through the dot operator. section 11.1.2

Go templates support several executable features:

  • Use {{.}} to print the current value, or use range and if to control how data is rendered. section 11.1.2
  • Register Go functions with template.FuncMap, then call them by name from template actions. section 11.5.2
  • Chain transformations with pipelines, passing one function’s output into the next with the | operator. section 11.5.2

Rendering habit status

A Go function can convert a habit’s weekly progress into labels such as not_started, started, good_progress, or completed. Register it as statusCSSClass, then render {{ statusCSSClass . }} inside a range loop to select the HTML element’s CSS class.

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains parsing, execution, actions, custom functions, and pipelines, but stops short of template-engine internals.
Examples
Uses habit counts, progression CSS labels, embedded HTML files, and URL-domain pipelines as concrete rendering examples.
What you can do after
Builds Go templates that render data, invoke registered functions, and chain transformations through pipelines.
How it connects
Connects templates with data models, actions, Go functions, pipelines, HTML, CSS, and URL parsing.

Also explained in

Compare how 5 books explain template engine →

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.