forked from julvo/htmlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (52 loc) · 1.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"log"
"net/http"
. "github.com/sdismo/htmlgo"
a "github.com/sdismo/htmlgo/attributes"
)
func main() {
http.HandleFunc("/", indexHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func indexHandler(w http.ResponseWriter, req *http.Request) {
fruit := []string{"Apple", "Banana", "Orange"}
fruitListItems := HTML("")
for _, f := range fruit {
fruitListItems += Li_(Text(f))
}
content :=
navbar(false) +
Ul_(fruitListItems) +
footer()
WriteTo(w, page("Home", content))
}
func page(title string, content HTML) HTML {
p :=
Html5_(
Head_(
Title_(Text(title)),
Meta(Attr(a.Charset_("utf-8"))),
Meta(Attr(a.Name_("viewport"), a.Content_("width=device-width"), a.InitialScale_("1"))),
Link(Attr(a.Rel_("stylesheet"), a.Href_("/static/css/main.min.css")))),
Body_(
content,
Script(Attr(a.Src_("/static/js/main.min.js")), JS{})))
return p
}
func navbar(isLoggedIn bool) HTML {
var navItems HTML
if !isLoggedIn {
navItems = A(Attr(a.Href_("/login")), Text_("Login"))
}
nav :=
Nav_(
Div_(navItems),
Hr_())
return nav
}
func footer() HTML {
return Footer_(
Hr_(),
Text_("© Acme Ltd, 2019"))
}