-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
65 lines (52 loc) · 1.26 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 (
"net/http"
"github.com/kataras/blocks"
)
var views = blocks.New("./views/public")
func main() {
err := views.Load()
if err != nil {
panic(err)
}
http.HandleFunc("/", index)
// Create a new Blocks set for the "admin".
adminViews := blocks.New("./views/admin")
if err = adminViews.Load(); err != nil {
panic(err)
}
// Two methods:
// 1. Wrapping the handler manually:
http.HandleFunc("/admin", admin(adminViews))
// 2. Using the `Set` middleware:
middleware := blocks.Set(adminViews)
http.Handle("/admin2", middleware(admin2{
Title: "Admin 2 Panel",
}))
println("Now listening on: http://localhost:8080")
http.ListenAndServe(":8080", nil)
}
func index(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{
"Title": "Page Title",
}
views.ExecuteTemplate(w, "index", "main", data)
}
func admin(v *blocks.Blocks) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{
"Title": "Admin Panel",
}
err := v.ExecuteTemplate(w, "index", "main", data)
if err != nil {
panic(err)
}
}
}
type admin2 struct {
Title string
}
func (h admin2) ServeHTTP(w http.ResponseWriter, r *http.Request) {
v := blocks.Get(r)
v.ExecuteTemplate(w, "index", "main", h)
}