Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.27 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "html/template"
  5.     "net/http"
  6. )
  7.  
  8. var tpl *template.Template
  9.  
  10. func init() { tpl = template.Must(template.ParseGlob("templates/*.gohtml")) }
  11.  
  12. type person struct {
  13.     Fname string
  14.     Lname string
  15.     Sub   bool
  16. }
  17.  
  18. func main() {
  19.     http.HandleFunc("/", index)
  20.     http.Handle("/favicon.ico", http.NotFoundHandler())
  21.     http.ListenAndServe(":8080", nil)
  22. }
  23.  
  24. func index(res http.ResponseWriter, req *http.Request) {
  25.     if req.Method == http.MethodPost {
  26.         f := req.FormValue("first")
  27.         l := req.FormValue("last")
  28.         s := req.FormValue("sub") == "on"
  29.         user := person{f, l, s}
  30.         tpl.ExecuteTemplate(res, "one.gohtml", user)
  31.     } else {
  32.         tpl.ExecuteTemplate(res, "one.gohtml", nil)
  33.     }
  34. }
  35. ==============================
  36. <html>
  37. <body>
  38.     <form method="Post">
  39.         first name: <input type="text" name="first"><br>
  40.         last name: <input type="text" name="last"><br>
  41.         sub : <input type="checkbox" name="sub"><br>
  42.         <input type="submit"><br>
  43.     </form>
  44.     {{if and .Fname .Lname}}
  45.         {{if and .Fname .Lname .Sub}}
  46.             <h4>{{.Fname}} {{.Lname}} - {{.Sub}}</h4>
  47.             {{template "two"}}
  48.         {{else}}
  49.             <h4>{{.Fname}} {{.Lname}}</h4>
  50.         {{end}}
  51.     {{else}}
  52.         <h4>Something is missing</h4>
  53.     {{end}}
  54. </body>
  55. </html>  
  56. {{define "two"}}
  57.     You filled all boxes, thank you.
  58. {{end}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement