Skip to content
/ muxt Public

"html/template" Enhancing Tools for Hypermedia Web Applications

License

Notifications You must be signed in to change notification settings

typelate/muxt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Muxt Go Reference Go

Generate HTTP handlers from html/template definitions.

Declare routes in template names using http.ServeMux patterns. Muxt analyzes receiver methods and generates handlers that parse parameters to match method signatures.

Muxt commands include:

  • muxt generate to generate http.Handler glue (see template_routes.go for the output)
  • muxt list template routes
  • muxt check to find bugs and more safely refactor your "text/template" or "html/template" source code
  • muxt list-template-calls list all call sites for a template (omit flag --patterns to list all call sites)
  • muxt list-template-callers list all callers for a template (omit flag --patterns to list all callers for all templates)

Syntax

Standard http.ServeMux pattern:

[METHOD ][HOST]/[PATH]

Muxt extends this with optional status codes and method calls:

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

Example

Define a template with a route pattern and method call:

{{define "GET /{id} GetUser(ctx, id)"}}
  {{with $err := .Err}}
    <div class="error" data-type="{{printf `%T` $err}}">{{$err.Error}}</div>
  {{else}}
    <h1>{{.Result.Name}}</h1>
    <p>{{.Result.Email}}</p>
  {{end}}
{{end}}

Implement the receiver method:

func (s Server) GetUser(ctx context.Context, id int) (User, error) {
    return s.db.GetUser(ctx, id)  // id automatically parsed from string
}

Run muxt generate --use-receiver-type=Server to generate HTTP handlers.

How It Works

Template names define the contract. Muxt analyzes method signatures using go/types and generates handlers that:

  • Parse path parameters to method argument types (string, int, bool, custom TextUnmarshaler)
  • Bind form data to struct fields with validation
  • Inject request context, *http.Request, or http.ResponseWriter when named
  • Handle errors and return values through TemplateData[T]
  • Set HTTP status codes from template names, return values, or error types

No (additional) runtime reflection. All type checking happens at generation time. The generated code uses only net/http and html/template from the standard library.

Installation

go install github.com/typelate/muxt@latest

Or add it to your project's module go get -tool github.com/typelate/muxt (note the project license documentation).

Quick Start

  1. Create a template file index.gohtml:
{{define "GET / Home(ctx)"}}
<!DOCTYPE html>
<html>
<body><h1>{{.Result}}</h1></body>
</html>
{{end}}
  1. Add generation directives to main.go:
//go:embed *.gohtml
var templateFS embed.FS

//go:generate muxt generate --use-receiver-type=Server
var templates = template.Must(template.ParseFS(templateFS, "*.gohtml"))

type Server struct{}

func (s Server) Home(ctx context.Context) string {
    return "Hello, Muxt!"
}
  1. Generate handlers and run:
go generate && go run .

Examples

The command tests were intended to be readable examples of muxt behavior.

Documentation

Comprehensive documentation organized by task:

See the full documentation index for all available resources.

Using with AI Assistants

Paste these prompts into Claude Code or other AI assistants when building hypermedia apps:

Prompt Use Case
muxt-quick.md Syntax lookup, minimal context
muxt-guide.md Comprehensive guide for building apps
muxt-complete.md Edge cases, testing patterns, advanced usage

Start with muxt-guide.md for most sessions. Use muxt-quick.md when context is limited.

License

Muxt generator: GNU AGPLv3

Generated code: MIT License - The Go code generated by Muxt is not covered by AGPL. It is provided as-is without warranty. Use it freely in your projects.