Skip to main content
Documentation
On this page

Install Cirth for Go templates

Go's standard library has a web server and HTML templates, and embed can put the templates and the stylesheet inside the binary. Cirth is one file among them. A shared head template links it once for every page. Gin, Echo and other routers serve static files and render html/template the same way, with their own calls.

Verified with Go 1.27.2 on

Five steps: add the file, link it from a template, serve both, build for production and check it.

1 Add the file #

In a module (go mod init example.com/app), next to main.go:

mkdir -p static/css templates
curl -o static/css/cirth.min.css \
  https://cdn.jsdelivr.net/npm/@cirthcss/cirth@0.17.0/dist/cirth.min.css

templates/head.html holds what every page's head shares:

{{define "head"}}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/css/cirth.min.css">
{{end}}

and templates/subscribe.html is a page that uses it:

<!doctype html>
<html lang="en">
  <head>
    {{template "head" .}}
    <title>Subscribe</title>
  </head>
  <body>
    <main class="container">
      <article>
        <header><h1>Hello, {{.Name}}</h1></header>
        <form method="post" action="/subscribe">
          <label>
            Email
            <input type="email" name="email" required>
          </label>
          <button type="submit">Subscribe</button>
        </form>
      </article>
    </main>
  </body>
</html>

html/template escapes what it prints for the place it prints it, so .Name cannot inject markup.

3 Serve both #

main.go embeds the two folders, serves static/ under /static/ and renders the page:

package main

import (
	"embed"
	"html/template"
	"io/fs"
	"log"
	"net/http"
)

//go:embed static templates
var files embed.FS

var pages = template.Must(template.ParseFS(files, "templates/*.html"))

func main() {
	static, err := fs.Sub(files, "static")
	if err != nil {
		log.Fatal(err)
	}
	http.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(static)))
	http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
		if err := pages.ExecuteTemplate(w, "subscribe.html", map[string]string{"Name": "Go"}); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}

4 Build for production #

go build -o app .
./app

The binary carries the stylesheet and the templates; copy it alone to the server.

5 Check it #

Put this forced-dark region in a page or component that uses the global stylesheet:

<section data-theme="dark">
  <article>
    <p>Dark, whatever the system scheme is.</p>
  </article>
</section>

Run the app, then open the page while the system uses a light theme. A dark article proves that Cirth loaded and that data-theme="dark" is working. It remains dark when the system switches to a dark theme.

Customization covers the --cirth-* properties; set yours in a stylesheet in static/css, linked after Cirth. Package and builds lists the classless, scoped and print entry points. Return to Framework guides for another stack.

Search documentation

Type at least two characters to search.