diff options
author | Markus Wüstenberg | 2020-10-28 16:59:04 +0100 |
---|---|---|
committer | GitHub | 2020-10-28 16:59:04 +0100 |
commit | 13701c4f668eba27956a8ac554a1fe272245d210 (patch) | |
tree | b123bbbedaa1fe8f666ce3981fead6e884dd0b74 /examples | |
parent | 18e90339fcac48806a5777766aeb256be2b8c4bc (diff) | |
download | gomponents-13701c4f668eba27956a8ac554a1fe272245d210.tar.lz gomponents-13701c4f668eba27956a8ac554a1fe272245d210.tar.zst gomponents-13701c4f668eba27956a8ac554a1fe272245d210.zip |
Add attribute helpers (#35)
Also refactor tests to be table-driven, for readability.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/simple/simple.go | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/examples/simple/simple.go b/examples/simple/simple.go index 56747e4..6683bc4 100644 --- a/examples/simple/simple.go +++ b/examples/simple/simple.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "net/http" "time" @@ -11,18 +10,14 @@ import ( ) func main() { - _ = http.ListenAndServe("localhost:8080", handler()) + _ = http.ListenAndServe("localhost:8080", http.HandlerFunc(handler)) } -func handler() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - props := pageProps{ - title: r.URL.Path, - path: r.URL.Path, - } - - _ = g.Write(w, page(props)) - } +func handler(w http.ResponseWriter, r *http.Request) { + _ = g.Write(w, page(pageProps{ + title: r.URL.Path, + path: r.URL.Path, + })) } type pageProps struct { @@ -32,11 +27,10 @@ type pageProps struct { func page(props pageProps) g.Node { return el.Document( - el.HTML( - g.Attr("lang", "en"), + el.HTML(attr.Lang("en"), el.Head( el.Title(props.title), - el.Style(g.Attr("type", "text/css"), + el.Style(attr.Type("text/css"), g.Raw(".is-active{font-weight: bold}"), g.Raw("ul.nav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; }"), g.Raw("ul.nav li { display: block; padding: 8px; float: left; }"), @@ -46,8 +40,8 @@ func page(props pageProps) g.Node { navbar(navbarProps{path: props.path}), el.Hr(), el.H1(props.title), - el.P(g.Text(fmt.Sprintf("Welcome to the page at %v.", props.path))), - el.P(g.Text(fmt.Sprintf("Rendered at %v", time.Now()))), + el.P(g.Textf("Welcome to the page at %v.", props.path)), + el.P(g.Textf("Rendered at %v", time.Now())), ), ), ) @@ -68,8 +62,9 @@ func navbar(props navbarProps) g.Node { } var lis []g.Node for _, item := range items { - lis = append(lis, el.Li( - el.A(item.path, attr.Classes(map[string]bool{"is-active": props.path == item.path}), g.Text(item.text)))) + lis = append(lis, el.Li(el.A(item.path, + attr.Classes(map[string]bool{"is-active": props.path == item.path}), + g.Text(item.text)))) } - return el.Ul(append(lis, attr.Class("nav"))...) + return el.Ul(attr.Class("nav"), g.Group(lis)) } |