← Back to archive

Repo of the Day

Phillip-England/templ-quickstart

Published: Mar 18, 2024

Open repository ↗

Scaffold an http server using Go, HTMX, Templ, and Tailwind quick and easy. I've been looking at learning Go more recently. It's fast, seems pretty simple to write and is strongly typed. Combining that with HTMX and Tailwind sounds like a cool stack to explore.

Summary

templ-quickstart is a scaffolding repository for building Go HTTP servers with Templ, HTMX, and Tailwind CSS. It provides a pre-configured layout with hot reloading via Air, a custom middleware chain, and a base template structure for rendering server-side HTML with htmx interactivity. The project is meant as a starting point engineers can clone and modify, not a framework to import.

What it is useful for

This template is useful when you want to build a server-rendered web application in Go without assembling the build pipeline yourself. Concretely, it helps with:

  • Prototyping small Go web apps that rely on HTMX for partial page updates instead of a heavier frontend stack.
  • Learning how Templ fits into a standard net/http server, since the repo shows how generated components are rendered from a handler.
  • Trying out Tailwind without configuring PostCSS or npm, because the standalone Tailwind CLI is used to produce output.css.
  • Experimenting with a flat middleware pattern that avoids deep nesting, via middleware.Chain.

The included example demonstrates a / route served by view.Home, a middleware.Chain function that accepts variadic middleware, and a Base templ component that wraps a page in <html> while loading HTMX 1.9.11 from unpkg and the generated Tailwind stylesheet.

How engineers can use it

The README documents a clear setup path. Clone the repository, enter the directory, and tidy modules:

git clone https://github.com/phillip-england/templ-quickstart <target-directory>
cd <target-directory>
go mod tidy

Create a .env file containing a PORT value (the README example uses PORT=8080). Before running, three binaries must already be on your PATH: the templ CLI, the standalone tailwindcss CLI, and air.

The three documented build and serve commands are:

templ generate --watch
tailwindcss -i ./static/css/input.css -o ./static/css/output.css --watch
air

The first generates Templ components, the second compiles Tailwind classes into static/css/output.css, and the third runs the server with hot reload. Drop --watch for a one-off build.

To add a page, define a new templ function in internal/template/, wrap its body with @Base(title), and add a route in main.go that calls middleware.Chain(w, r, view.YourHandler). Middleware is added by appending functions to the variadic Chain call, as long as they match the CustomMiddleware signature and either return nil or write an early response.

Documented limitations: Go 1.22.0 or greater is required because routing uses the mux.HandleFunc("GET /", ...) syntax, and three external CLI binaries must be installed before the project will build. The README does not list a license, so check the repository before reuse.