mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
Add some _examples in the main repository too.
Former-commit-id: 98895c34115ec2076b431332f0ffe9645adf7590
This commit is contained in:
47
_examples/examples/forms/main.go
Normal file
47
_examples/examples/forms/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
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")
|
||||
}
|
||||
Reference in New Issue
Block a user