Skip to content

Repository files navigation

Muxt Go Reference

Build with html/template power tools: http.Handler generation and static type-checking of *.gohtml files.

{{define "GET /article/{id} GetArticle(ctx, id)"}}
  <h1>{{.Result.Title}}</h1>
{{end}}
func (s Server) GetArticle(ctx context.Context, id int) (Article, error) { ... }

The template name is the route, the handler call, and the parameter list. Muxt reads it, parses the request into those arguments, calls your method, and renders the result.

You can stay in hypermedia-land and drive a significant amount of behavior from your template source files.

Muxt plays super well with fixi, htmx, Datastar, and plain server-rendered HTML.

Making templates statically analyzable was a key development goal of Muxt and the original inspiration. The generated handlers call ExecuteTemplate with a string-literal template name and data of a known static type, so muxt check can type-check each template and its actions against the data it actually receives.

Install

go install github.com/typelate/muxt@latest

Or as a project tool: go get -tool github.com/typelate/muxt. Pre-built binaries are also attached to each release.

muxt loads Go packages the way the go command does, so it inherits GOWORK, GOFLAGS, and GOROOT from its environment.

Template Name Syntax

This is what muxt uses to map Go <-> HTML.

Standard http.ServeMux pattern, optionally with a status code and method call:

[METHOD ][HOST]/[PATH][ HTTP_STATUS][ CALL]

Example: "POST /user/{id} 201 CreateUser(ctx, id, form)"

Supported parameters: ctx, request, response, path params, form (URL-encoded body), multipart (file uploads, including *multipart.FileHeader fields), body (raw io.Reader), unmarshalJSON(body) (JSON request bodies), execute (render callback), and lastEventID. Returns and errors flow through TemplateData[R, T]. Status codes can come from the template name, return values, or error types.

Wrapping the call changes the response representation: sse(Stream(ctx, execute)) streams Server-Sent Events and marshalJSON(GetUser(ctx)) responds application/json. Generate flags select a frontend library per package — --output-htmx adds HX* helper methods to TemplateData, and --output-datastar frames SSE events with Datastar's patch-elements protocol.

TemplateRoutePaths extends type safety to URLs: {{$.Path.GetArticle 42}} instead of hardcoded href="/article/42". Change the route pattern, the compiler finds every stale reference.

Examples

The command tests double as readable examples of every feature.

Commands

  • muxt generate: generate http.Handler glue (writes template_routes.go)
  • muxt check: type-check templates without generating (use in CI or editor save hooks)
  • muxt list-template-calls / muxt list-template-callers: explore call sites and callers
  • muxt explore-module / muxt generate-fake-server: survey muxt packages and spin up a fake server to poke at routes

Documentation

See the full documentation index.

Go Standard Library

License

Muxt generator: GNU AGPLv3

Generated code: not covered by AGPL. Muxt asserts no copyright over its output. Treat generated files as your own code, under whatever license your project uses.

No warranty. Muxt and its generated output are provided "as is", without warranty of any kind, express or implied, including but not limited to warranties of merchantability, fitness for a particular purpose, and non-infringement. You are responsible for reviewing, testing, and securing any code generated by Muxt before using it. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability arising from the use of Muxt or its output.

Releases

Used by

Contributors

Languages