gomponents

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2020 License: MIT Imports: 4 Imported by: 0

README

gomponents

GoDoc codecov

gomponents are declarative view components in Go, that can render to HTML. gomponents aims to make it easy to build HTML pages of reusable components, without the use of a template language. Think server-side-rendered React, but without the virtual DOM and diffing.

The implementation is still incomplete, but usable. The API may change until version 1 is reached.

Check out the blog post gomponents: declarative view components in Go for background.

Usage

Get the library using go get:

go get -u github.com/maragudk/gomponents

Then do something like this:

package main

import (
	"net/http"

	g "github.com/maragudk/gomponents"
	"github.com/maragudk/gomponents/attr"
	"github.com/maragudk/gomponents/el"
)

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

func handler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		page := Page("Hi!", r.URL.Path)
		_ = g.Write(w, page)
	}
}

func Page(title, path string) g.Node {
	return el.Document(
		el.HTML(
			g.Attr("lang", "en"),
			el.Head(
				el.Title(title),
				el.Style(g.Attr("type", "text/css"), g.Raw(".is-active{font-weight: bold}")),
			),
			el.Body(
				Navbar(path),
				el.H1(title),
				el.P(g.Textf("Welcome to the page at %v.", path)),
			),
		),
	)
}

func Navbar(path string) g.Node {
	return g.El("nav",
		el.A("/", attr.Classes{"is-active": path == "/"}, g.Text("Home")),
		el.A("/about", attr.Classes{"is-active": path == "/about"}, g.Text("About")),
	)
}

You could also use a page template to simplify your code a bit:

package main

import (
	"net/http"

	g "github.com/maragudk/gomponents"
	"github.com/maragudk/gomponents/attr"
	c "github.com/maragudk/gomponents/components"
	"github.com/maragudk/gomponents/el"
)

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

func handler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		page := Page("Hi!", r.URL.Path)
		_ = g.Write(w, page)
	}
}

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

func Navbar(path string) g.Node {
	return g.El("nav",
		el.A("/", attr.Classes{"is-active": path == "/"}, g.Text("Home")),
		el.A("/about", attr.Classes{"is-active": path == "/about"}, g.Text("About")),
	)
}

For more complete examples, see the examples directory.

Documentation

Overview

Package gomponents provides declarative view components in Go, that can render to HTML. The primary interface is a Node, which has a single function Render, which should render the Node to a string. Furthermore, NodeFunc is a function which implements the Node interface by calling itself on Render. All DOM elements and attributes can be created by using the El and Attr functions. The package also provides a lot of convenience functions for creating elements and attributes with the most commonly used parameters. If they don't suffice, a fallback to El and Attr is always possible.

Index

Constants

View Source
const (
	Outside = Placement(iota)
	Inside
)

Variables

This section is empty.

Functions

func Write added in v0.3.0

func Write(w io.Writer, n Node) error

Write to the given io.Writer, returning any error.

Types

type Node

type Node interface {
	Render() string
}

Node is a DOM node that can Render itself to a string representation.

func Attr

func Attr(name string, value ...string) Node

Attr creates an attr DOM Node. If one parameter is passed, it's a name-only attribute (like "required"). If two parameters are passed, it's a name-value attribute (like `class="header"`). More parameter counts make Attr panic. Use this if no convenience creator exists.

func Group added in v0.7.0

func Group(children []Node) Node

Group multiple Nodes into one Node. Useful for concatenation of Nodes in variadic functions. The resulting Node cannot Render directly, trying it will panic. Render must happen through a parent element created with El or a helper.

type NodeFunc

type NodeFunc func() string

NodeFunc is render function that is also a Node.

func El

func El(name string, children ...Node) NodeFunc

El creates an element DOM Node with a name and child Nodes. Use this if no convenience creator exists.

func Raw

func Raw(t string) NodeFunc

Raw creates a raw Node that just Renders the unescaped string t.

func Text

func Text(t string) NodeFunc

Text creates a text DOM Node that Renders the escaped string t.

func Textf added in v0.4.0

func Textf(format string, a ...interface{}) NodeFunc

Textf creates a text DOM Node that Renders the interpolated and escaped string t.

func (NodeFunc) Place added in v0.5.0

func (n NodeFunc) Place() Placement

func (NodeFunc) Render

func (n NodeFunc) Render() string

func (NodeFunc) String added in v0.2.0

func (n NodeFunc) String() string

String satisfies fmt.Stringer.

type Placement added in v0.5.0

type Placement int

Placement is used with the Placer interface.

type Placer added in v0.5.0

type Placer interface {
	Place() Placement
}

Placer can be implemented to tell Render functions where to place the string representation of a Node in the parent element.

Directories

Path Synopsis
Package attr provides shortcuts and helpers to common HTML attributes.
Package attr provides shortcuts and helpers to common HTML attributes.
Package el provides shortcuts and helpers to common HTML elements.
Package el provides shortcuts and helpers to common HTML elements.
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL