• Stars
    star
    953
  • Rank 47,957 (Top 1.0 %)
  • Language
    Go
  • License
    MIT License
  • Created about 4 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTML components in pure Go.

Tired of complex template languages?

GoDoc Go codecov Go Report Card

Try view components in pure Go.

gomponents are view components written in pure Go. They render to HTML 5, and make it easy for you to build reusable components. So you can focus on building your app instead of learning yet another templating language.

The API may change until version 1 is reached.

Check out www.gomponents.com for an introduction.

Made in πŸ‡©πŸ‡° by maragu, maker of online Go courses.

Features

  • Build reusable view components
  • Write declarative HTML5 in Go without all the strings, so you get
    • Type safety
    • Auto-completion
    • Nice formatting with gofmt
  • Simple API that's easy to learn and use (you know most already if you know HTML)
  • Useful helpers like Text and Textf that insert HTML-escaped text, Map for mapping data to components, and If for conditional rendering.
  • No external dependencies

Usage

Get the library using go get:

go get github.com/maragudk/gomponents

The preferred way to use gomponents is with so-called dot-imports (note the dot before the gomponents/html import), to give you that smooth, native HTML feel:

package main

import (
	"net/http"

	g "github.com/maragudk/gomponents"
	c "github.com/maragudk/gomponents/components"
	. "github.com/maragudk/gomponents/html"
)

func main() {
	_ = http.ListenAndServe("localhost:8080", http.HandlerFunc(handler))
}

func handler(w http.ResponseWriter, r *http.Request) {
	_ = Page("Hi!", r.URL.Path).Render(w)
}

func Page(title, currentPath string) g.Node {
	return Doctype(
		HTML(
			Lang("en"),
			Head(
				TitleEl(g.Text(title)),
				StyleEl(Type("text/css"), g.Raw(".is-active{ font-weight: bold }")),
			),
			Body(
				Navbar(currentPath),
				H1(g.Text(title)),
				P(g.Textf("Welcome to the page at %v.", currentPath)),
			),
		),
	)
}

func Navbar(currentPath string) g.Node {
	return Nav(
		NavbarLink("/", "Home", currentPath),
		NavbarLink("/about", "About", currentPath),
	)
}

func NavbarLink(href, name, currentPath string) g.Node {
	return A(Href(href), c.Classes{"is-active": currentPath == href}, g.Text(name))
}

Some people don't like dot-imports, and luckily it's completely optional. If you don't like dot-imports, just use regular imports.

You could also use the provided HTML5 document template to simplify your code a bit:

package main

import (
	"net/http"

	g "github.com/maragudk/gomponents"
	c "github.com/maragudk/gomponents/components"
	. "github.com/maragudk/gomponents/html"
)

func main() {
	_ = http.ListenAndServe("localhost:8080", http.HandlerFunc(handler))
}

func handler(w http.ResponseWriter, r *http.Request) {
	_ = Page("Hi!", r.URL.Path).Render(w)
}

func Page(title, currentPath string) g.Node {
	return c.HTML5(c.HTML5Props{
		Title:    title,
		Language: "en",
		Head: []g.Node{
			StyleEl(Type("text/css"), g.Raw(".is-active{ font-weight: bold }")),
		},
		Body: []g.Node{
			Navbar(currentPath),
			H1(g.Text(title)),
			P(g.Textf("Welcome to the page at %v.", currentPath)),
		},
	})
}

func Navbar(currentPath string) g.Node {
	return Nav(
		NavbarLink("/", "Home", currentPath),
		NavbarLink("/about", "About", currentPath),
	)
}

func NavbarLink(href, name, currentPath string) g.Node {
	return A(Href(href), c.Classes{"is-active": currentPath == href}, g.Text(name))
}

For more complete examples, see the examples directory.

What's up with the specially named elements and attributes?

Unfortunately, there are four main name clashes in HTML elements and attributes, so they need an El or Attr suffix, respectively, to be able to co-exist in the same package in Go:

  • data (DataEl/DataAttr)
  • form (FormEl/FormAttr)
  • style (StyleEl/StyleAttr)
  • title (TitleEl/TitleAttr)

More Repositories

1

goqite

Go queue library built on SQLite and inspired by AWS SQS.
Go
409
star
2

gomponents-htmx

HTMX attributes and helpers for gomponents.
Go
77
star
3

migrate

A simple database migration tool.
Go
50
star
4

sqlite-app

Companion app to "The Complete Guide to Go and SQLite".
Go
39
star
5

gomponents.com

Website for the gomponents library.
Go
15
star
6

service

A template for Go services.
Go
14
star
7

gomponents-heroicons

gomponents + heroicons
Go
13
star
8

go-ahead

A SaaS web app starter template written in Go.
Go
11
star
9

gomponents-tailwind-example

This projects shows how to use gomponents together with TailwindCSS.
CSS
11
star
10

httph

HTTP helpers and middleware. H is for helpful!
Go
10
star
11

dblens

An HTTP handler for database browsing.
Go
7
star
12

template

A template for Go projects.
Makefile
6
star
13

env

A small utility package to load different types of environment variables.
Go
6
star
14

go-bench2csv

A small CLI to parse the output of go test -bench and output to CSV.
Go
6
star
15

ssssg

Super Simple Static Site Generator 😎
Go
5
star
16

certmagic-storage-crdb

An implementation of certmagic's Storage interface for CockroachDB.
Go
5
star
17

rest-auth-server

An example HTTP REST server with authentication and authorization.
Go
5
star
18

sqlite-benchmark

Companion repo for "Benchmarking SQLite Performance in Go"
Go
4
star
19

aws

Simple AWS SDK helpers.
Go
4
star
20

http-handler-testing

How to structure and test HTTP handlers in Go.
Go
3
star
21

logtailpipe

Pipe STDIN to Logtail over HTTP.
Go
3
star
22

podcastgenerator

A work-in-progress podcast generator in Go.
Makefile
2
star
23

sqlite

An experimental SQLite database driver.
C
2
star
24

podcasthost

A work-in-progress podcast host in Go.
Go
2
star
25

minio-ci

A minimal layer on top of minio/minio:latest to use the CMD "minio server /data".
Makefile
2
star
26

snorkel

Scuba for the rest of us.
Go
2
star
27

is

A testing helper.
Go
2
star
28

errors

errors provides Wrap in addition to the functions in the stdlib errors package.
Go
2
star
29

doessqlitescale.com

Website for doessqlitescale.com
Go
2
star
30

llamafile

Scripts to create llamafiles.
Roff
2
star
31

goat

goat the LLM CLI
Go
1
star
32

goo

The goo to glue web services together.
Go
1
star
33

catchytuna

Federated music distribution platform.
Go
1
star
34

gomponents-email

HTML email templates using gomponents.
1
star
35

gomponents-starter-kit

Makefile
1
star
36

gomponents-tailwind

gomponents-tailwind are some standard components using gomponents and Tailwind CSS.
Go
1
star
37

gomponents-language-server

Language server for LSP for gomponents.
Makefile
1
star
38

ihukom

Go
1
star
39

dotfiles

Shell
1
star