tmplx
tmplx logo

tmplx

Write Go in HTML

A page is one HTML file with a Go script block. State is a var, handlers are Go statements in attributes, and every handler runs on the server. tmplx compiles it all into a single routes.go on net/http — there is no JavaScript to write.

A query in your HTML

Type below. Every keystroke posts the state up, runs albums.Search(q) on the server, and morphs the rows that come back. results is derived from q, so it recomputes whenever q changes.

Kind of Blue Miles Davis 1959
A Love Supreme John Coltrane 1965
Abbey Road The Beatles 1969
The Dark Side of the Moon Pink Floyd 1973
Rumours Fleetwood Mac 1977
Thriller Michael Jackson 1982
Purple Rain Prince 1984
Nevermind Nirvana 1991
OK Computer Radiohead 1997
Discovery Daft Punk 2001
components/search.html
source

albums.Search is an ordinary Go function in an ordinary Go package — an in-memory table here, your real database in production.

The tmplx way

Demos

Counter

State, rendered and mutated, in five lines.

0
components/counter.html
source

To Do

A form posts straight to a Go function; click an item to remove it.

components/todo.html
source

From file to server

  1. Install the compiler: go install github.com/gnituy18/tmplx@latest
  2. Write pages in pages/ and components in components/.
  3. Run tmplx — it type-checks every file and generates routes.go. Then go run .