New projects default to a React frontend with Inertia; see Frontend. This page covers classic server-rendered Go templates, which are a great fit for simpler apps and HTMX-style interactivity.
Template Directory
Templates live underweb/src in three conventional directories:
web/src/layouts/main.html
web/src/pages/rocket.html
Rendering Pages
Copper provideschttp.HTMLReaderWriter for writing HTML responses. Inject it into your router and call WriteHTML:
LayoutTemplate defaults to main.html and StatusCode to 200; you may set either explicitly when needed. On errors, an appropriate page will automatically be chosen for you: internal-error.html for 500s and not-found.html for 404s.
Template Functions
Every template has the full Sprig function library available, includingupper, date, and mustToRawJson.
You may add your own functions using chttp.HTMLRenderFunc. Since render funcs receive the current request, they can expose per-request state such as the signed-in user:
web/wire.go
Partials
Partials inweb/src/partials are reusable fragments. Render one inside any template with the built-in partial function:
Static Assets
Files inweb/public are served under /static/. For example, web/public/logo.svg is available at /static/logo.svg. In production these files are embedded into your binary, while in development they are read from disk.
Local Development
Two config flags make template development pleasant, and both are enabled inconfig/dev.toml for new projects:
Single-Page Apps
If your frontend handles its own routing, you may configure every unmatched GET request to renderindex.html:
API-Only Apps
If your application doesn’t serve HTML at all, usechttp.WireModuleEmptyHTML in place of the HTML and static dir bindings; it satisfies wire with empty stand-ins. Projects created with -frontend=none are configured this way.