forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (64 loc) · 1.95 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
75
76
77
78
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
)
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
// Init the handlebars engine
e := iris.Handlebars("./templates", ".html").Reload(true)
// Register a helper.
e.AddFunc("fullName", func(person map[string]string) string {
return person["firstName"] + " " + person["lastName"]
})
app.RegisterView(e)
app.Get("/", func(ctx iris.Context) {
viewData := iris.Map{
"author": map[string]string{"firstName": "Jean", "lastName": "Valjean"},
"body": "Life is difficult",
"comments": []iris.Map{{
"author": map[string]string{"firstName": "Marcel", "lastName": "Beliveau"},
"body": "LOL!",
}},
}
if err := ctx.View("example.html", viewData); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
exampleRouter := app.Party("/example")
/* See context-view-data example: Set data through one or more middleware */
exampleRouter.Use(func(ctx iris.Context) {
ctx.ViewData("author", map[string]string{"firstName": "Jean", "lastName": "Valjean"})
ctx.ViewData("body", "Life is difficult")
ctx.ViewData("comments", []iris.Map{{
"author": map[string]string{"firstName": "Marcel", "lastName": "Beliveau"},
"body": "LOL!",
}})
// OR:
// ctx.ViewData("", iris.Map{
// "author": map[string]string{"firstName": "Jean", "lastName": "Valjean"},
// "body": "Life is difficult",
// "comments": []iris.Map{{
// "author": map[string]string{"firstName": "Marcel", "lastName": "Beliveau"},
// "body": "LOL!",
// }},
// })
ctx.Next()
})
mvc.New(exampleRouter).Handle(new(controller))
// Read more about its syntax at:
// https://github.com/mailgun/raymond and
// https://handlebarsjs.com/guide
// http://localhost:8080
// http://localhost:8080/example
app.Listen(":8080")
}
type controller struct{}
func (c *controller) Get() mvc.Result {
return mvc.View{
Name: "example",
Code: 200,
}
}