diff options
author | Markus Wüstenberg | 2020-10-29 12:03:43 +0100 |
---|---|---|
committer | GitHub | 2020-10-29 12:03:43 +0100 |
commit | 3df42084aed213fc9e760b53b5132da63b1818b7 (patch) | |
tree | 51fbb6cc46f71762b711012a4455d0d228f992ad | |
parent | 13701c4f668eba27956a8ac554a1fe272245d210 (diff) | |
download | gomponents-3df42084aed213fc9e760b53b5132da63b1818b7.tar.lz gomponents-3df42084aed213fc9e760b53b5132da63b1818b7.tar.zst gomponents-3df42084aed213fc9e760b53b5132da63b1818b7.zip |
Add HTML5 document template (#36)
-rw-r--r-- | README.md | 46 | ||||
-rw-r--r-- | attr/simple.go | 8 | ||||
-rw-r--r-- | attr/simple_test.go | 2 | ||||
-rw-r--r-- | components/documents.go | 40 | ||||
-rw-r--r-- | components/documents_test.go | 33 |
5 files changed, 129 insertions, 0 deletions
diff --git a/README.md b/README.md index 6fe9ca8..b976f42 100644 --- a/README.md +++ b/README.md @@ -70,4 +70,50 @@ func Navbar(path string) g.Node { } ``` +You could also use a page template to simplify your code a bit: + +```go +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](examples/). diff --git a/attr/simple.go b/attr/simple.go index e286c26..d7d4ab8 100644 --- a/attr/simple.go +++ b/attr/simple.go @@ -12,10 +12,18 @@ func AutoComplete(v string) g.Node { return g.Attr("autocomplete", v) } +func Charset(v string) g.Node { + return g.Attr("charset", v) +} + func Class(v string) g.Node { return g.Attr("class", v) } +func Content(v string) g.Node { + return g.Attr("content", v) +} + func Form(v string) g.Node { return g.Attr("form", v) } diff --git a/attr/simple_test.go b/attr/simple_test.go index 784561a..eb2cfe3 100644 --- a/attr/simple_test.go +++ b/attr/simple_test.go @@ -13,7 +13,9 @@ func TestSimpleAttributes(t *testing.T) { cases := map[string]func(string) g.Node{ "accept": attr.Accept, "autocomplete": attr.AutoComplete, + "charset": attr.Charset, "class": attr.Class, + "content": attr.Content, "form": attr.Form, "height": attr.Height, "href": attr.Href, diff --git a/components/documents.go b/components/documents.go new file mode 100644 index 0000000..65f3e63 --- /dev/null +++ b/components/documents.go @@ -0,0 +1,40 @@ +package components + +import ( + g "github.com/maragudk/gomponents" + "github.com/maragudk/gomponents/attr" + "github.com/maragudk/gomponents/el" +) + +// DocumentProps for HTML5. +// Title is set no matter what, Description and Language elements only if the strings are non-empty. +type DocumentProps struct { + Title string + Description string + Language string + Head []g.Node + Body []g.Node +} + +// HTML5 document template. +func HTML5(p DocumentProps) g.NodeFunc { + var lang, description g.Node + if p.Language != "" { + lang = attr.Lang(p.Language) + } + if p.Description != "" { + description = el.Meta(attr.Name("description"), attr.Content(p.Description)) + } + return el.Document( + el.HTML(lang, + el.Head( + el.Meta(attr.Charset("utf-8")), + el.Meta(attr.Name("viewport"), attr.Content("width=device-width, initial-scale=1")), + el.Title(p.Title), + description, + g.Group(p.Head), + ), + el.Body(g.Group(p.Body)), + ), + ) +} diff --git a/components/documents_test.go b/components/documents_test.go new file mode 100644 index 0000000..2aabd5a --- /dev/null +++ b/components/documents_test.go @@ -0,0 +1,33 @@ +package components_test + +import ( + "testing" + + g "github.com/maragudk/gomponents" + "github.com/maragudk/gomponents/assert" + "github.com/maragudk/gomponents/attr" + c "github.com/maragudk/gomponents/components" + "github.com/maragudk/gomponents/el" +) + +func TestHTML5(t *testing.T) { + t.Run("returns an html5 document template", func(t *testing.T) { + e := c.HTML5(c.DocumentProps{ + Title: "Hat", + Description: "Love hats.", + Language: "en", + Head: []g.Node{el.Link(attr.Rel("stylesheet"), attr.Href("/hat.css"))}, + Body: []g.Node{el.Div()}, + }) + + assert.Equal(t, `<!doctype html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>Hat</title><meta name="description" content="Love hats." /><link rel="stylesheet" href="/hat.css" /></head><body><div /></body></html>`, e) + }) + + t.Run("returns no language, description, and extra head/body elements if empty", func(t *testing.T) { + e := c.HTML5(c.DocumentProps{ + Title: "Hat", + }) + + assert.Equal(t, `<!doctype html><html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>Hat</title></head><body /></html>`, e) + }) +} |