-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathreadme.go
52 lines (46 loc) · 1.18 KB
/
readme.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"html/template"
"net/http"
"github.com/joncalhoun/form"
)
var inputTpl = `
<label {{with .ID}}for="{{.}}"{{end}}>
{{.Label}}
</label>
<input {{with .ID}}id="{{.}}"{{end}} type="{{.Type}}" name="{{.Name}}" placeholder="{{.Placeholder}}" {{with .Value}}value="{{.}}"{{end}}>
{{with .Footer}}
<p>{{.}}</p>
{{end}}
`
// Address is an example type used to demonstrate the form package.
type Address struct {
Street1 string `form:"label=Street;placeholder=123 Sample St"`
Street2 string `form:"label=Street (cont);placeholder=Apt 123"`
City string
State string `form:"footer=Or your Province"`
Zip string `form:"label=Postal Code"`
Country string
}
func main() {
tpl := template.Must(template.New("").Parse(inputTpl))
fb := form.Builder{
InputTemplate: tpl,
}
pageTpl := template.Must(template.New("").Funcs(fb.FuncMap()).Parse(`
<html>
<body>
<form>
{{inputs_for .}}
</form>
</body>
</html>`))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
pageTpl.Execute(w, Address{
Street1: "123 Known St",
Country: "United States",
})
})
http.ListenAndServe(":3000", nil)
}