Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "html/template"
- "net/http"
- )
- var tpl *template.Template
- func init() { tpl = template.Must(template.ParseGlob("templates/*.gohtml")) }
- type person struct {
- Fname string
- Lname string
- Sub bool
- }
- func main() {
- http.HandleFunc("/", index)
- http.Handle("/favicon.ico", http.NotFoundHandler())
- http.ListenAndServe(":8080", nil)
- }
- func index(res http.ResponseWriter, req *http.Request) {
- if req.Method == http.MethodPost {
- f := req.FormValue("first")
- l := req.FormValue("last")
- s := req.FormValue("sub") == "on"
- user := person{f, l, s}
- tpl.ExecuteTemplate(res, "one.gohtml", user)
- } else {
- tpl.ExecuteTemplate(res, "one.gohtml", nil)
- }
- }
- ==============================
- <html>
- <body>
- <form method="Post">
- first name: <input type="text" name="first"><br>
- last name: <input type="text" name="last"><br>
- sub : <input type="checkbox" name="sub"><br>
- <input type="submit"><br>
- </form>
- {{if and .Fname .Lname}}
- {{if and .Fname .Lname .Sub}}
- <h4>{{.Fname}} {{.Lname}} - {{.Sub}}</h4>
- {{template "two"}}
- {{else}}
- <h4>{{.Fname}} {{.Lname}}</h4>
- {{end}}
- {{else}}
- <h4>Something is missing</h4>
- {{end}}
- </body>
- </html>
- {{define "two"}}
- You filled all boxes, thank you.
- {{end}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement