2020-08-30 14:26:50 +02:00
|
|
|
package main
|
|
|
|
|
2020-08-30 16:18:04 +02:00
|
|
|
import (
|
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/mvc"
|
|
|
|
)
|
2020-08-30 14:26:50 +02:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
2020-08-30 15:14:32 +02:00
|
|
|
app.Logger().SetLevel("debug")
|
2020-08-30 14:26:50 +02:00
|
|
|
|
|
|
|
// Init the handlebars engine
|
2020-09-08 06:55:33 +02:00
|
|
|
e := iris.Handlebars("./templates", ".html").Reload(true)
|
2020-08-30 14:26:50 +02:00
|
|
|
// Register a helper.
|
2020-09-08 06:55:33 +02:00
|
|
|
e.AddFunc("fullName", func(person map[string]string) string {
|
2020-08-30 14:26:50 +02:00
|
|
|
return person["firstName"] + " " + person["lastName"]
|
|
|
|
})
|
|
|
|
|
2020-09-08 06:55:33 +02:00
|
|
|
app.RegisterView(e)
|
2020-08-30 14:26:50 +02:00
|
|
|
|
|
|
|
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!",
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2022-12-13 00:37:15 +01:00
|
|
|
if err := ctx.View("example.html", viewData); err != nil {
|
|
|
|
ctx.HTML("<h3>%s</h3>", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-08-30 14:26:50 +02:00
|
|
|
})
|
|
|
|
|
2020-08-30 16:18:04 +02:00
|
|
|
exampleRouter := app.Party("/example")
|
|
|
|
/* See context-view-data example: Set data through one or more middleware */
|
|
|
|
exampleRouter.Use(func(ctx iris.Context) {
|
2020-08-30 15:14:32 +02:00
|
|
|
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!",
|
|
|
|
}})
|
|
|
|
|
2020-08-30 16:18:04 +02:00
|
|
|
// 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!",
|
|
|
|
// }},
|
|
|
|
// })
|
|
|
|
|
2020-08-30 15:14:32 +02:00
|
|
|
ctx.Next()
|
|
|
|
})
|
2020-08-30 16:18:04 +02:00
|
|
|
|
|
|
|
mvc.New(exampleRouter).Handle(new(controller))
|
2020-08-30 15:14:32 +02:00
|
|
|
|
2020-08-30 14:26:50 +02:00
|
|
|
// Read more about its syntax at:
|
2022-03-18 21:25:22 +01:00
|
|
|
// https://github.com/mailgun/raymond and
|
2020-08-30 14:26:50 +02:00
|
|
|
// https://handlebarsjs.com/guide
|
2020-08-30 16:18:04 +02:00
|
|
|
|
|
|
|
// http://localhost:8080
|
|
|
|
// http://localhost:8080/example
|
2020-08-30 14:26:50 +02:00
|
|
|
app.Listen(":8080")
|
|
|
|
}
|
2020-08-30 16:18:04 +02:00
|
|
|
|
|
|
|
type controller struct{}
|
|
|
|
|
|
|
|
func (c *controller) Get() mvc.Result {
|
|
|
|
return mvc.View{
|
|
|
|
Name: "example",
|
|
|
|
Code: 200,
|
|
|
|
}
|
|
|
|
}
|