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 /README.md | |
parent | 13701c4f668eba27956a8ac554a1fe272245d210 (diff) | |
download | gomponents-3df42084aed213fc9e760b53b5132da63b1818b7.tar.lz gomponents-3df42084aed213fc9e760b53b5132da63b1818b7.tar.zst gomponents-3df42084aed213fc9e760b53b5132da63b1818b7.zip |
Add HTML5 document template (#36)
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 46 |
1 files changed, 46 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/). |