iris/_examples/examples/forms/main.go
Gerasimos (Makis) Maropoulos f487cd0029 Add some _examples in the main repository too.
Former-commit-id: 98895c34115ec2076b431332f0ffe9645adf7590
2017-03-13 15:16:12 +02:00

48 lines
1.0 KiB
Go

package main
import (
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/adaptors/view"
)
// ContactDetails the information from user
type ContactDetails struct {
Email string `form:"email"`
Subject string `form:"subject"`
Message string `form:"message"`
}
func main() {
app := iris.New()
app.Adapt(httprouter.New())
// Parse all files inside `./mytemplates` directory ending with `.html`
app.Adapt(view.HTML("./mytemplates", ".html"))
app.Get("/", func(ctx *iris.Context) {
ctx.Render("forms.html", nil)
})
// Equivalent with app.HandleFunc("POST", ...)
app.Post("/", func(ctx *iris.Context) {
// details := ContactDetails{
// Email: ctx.FormValue("email"),
// Subject: ctx.FormValue("subject"),
// Message: ctx.FormValue("message"),
// }
// or simply:
var details ContactDetails
ctx.ReadForm(&details)
// do something with details
_ = details
ctx.Render("forms.html", struct{ Success bool }{true})
})
app.Listen(":8080")
}