-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
74 lines (59 loc) · 1.6 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
66
67
68
69
70
71
72
73
74
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
// Register a root view engine, as usual,
// will be used to render files through Context.View method
// when no Party or Handler-specific view engine is available.
app.RegisterView(iris.Blocks("./views/public", ".html"))
// http://localhost:8080
app.Get("/", index)
// Register a view engine per group of routes.
adminGroup := app.Party("/admin")
adminGroup.RegisterView(iris.Blocks("./views/admin", ".html"))
// http://localhost:8080/admin
adminGroup.Get("/", admin)
// Register a view engine on-fly for the current chain of handlers.
views := iris.Blocks("./views/on-fly", ".html")
if err := views.Load(); err != nil {
app.Logger().Fatal(err)
}
// http://localhost:8080/on-fly
app.Get("/on-fly", setViews(views), onFly)
app.Listen(":8080")
}
func index(ctx iris.Context) {
data := iris.Map{
"Title": "Public Index Title",
}
ctx.ViewLayout("main")
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
func admin(ctx iris.Context) {
data := iris.Map{
"Title": "Admin Panel",
}
ctx.ViewLayout("main")
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
func setViews(views iris.ViewEngine) iris.Handler {
return func(ctx iris.Context) {
ctx.ViewEngine(views)
ctx.Next()
}
}
func onFly(ctx iris.Context) {
data := iris.Map{
"Message": "View engine changed through 'setViews' custom middleware.",
}
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}