1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-21 11:57:02 +00:00

Add some _examples in the main repository too.

Former-commit-id: 98895c34115ec2076b431332f0ffe9645adf7590
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-03-13 15:16:12 +02:00
parent d76d9b1ec6
commit f487cd0029
29 changed files with 1318 additions and 248 deletions

View 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")
}

View File

@@ -0,0 +1,14 @@
{{if .Success}}
<h1>Thanks for your message!</h1>
{{else}}
<h1>Contact</h1>
<form method="POST">
<label>Email:</label><br />
<input type="text" name="email"><br />
<label>Subject:</label><br />
<input type="text" name="subject"><br />
<label>Message:</label><br />
<textarea name="message"></textarea><br />
<input type="submit">
</form>
{{end}}