diff --git a/README.md b/README.md index 0dfdfd5..eaccd3a 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ var inputTpl = ` - + {{with .Footer}}
{{.}}
{{end}} @@ -149,7 +149,7 @@ type Address struct { City string State string `form:"footer=Or your Province"` Zip string `form:"label=Postal Code"` - Country string + Country string `form:"class=specific-country-css-class"` } func main() { @@ -211,7 +211,7 @@ func main() { - + ``` diff --git a/builder_test.go b/builder_test.go index d360a1d..a01d8b1 100644 --- a/builder_test.go +++ b/builder_test.go @@ -11,7 +11,7 @@ import ( func TestBuilder_Inputs(t *testing.T) { tpl := template.Must(template.New("").Parse(strings.TrimSpace(` - + `))) tests := []struct { name string @@ -25,14 +25,18 @@ func TestBuilder_Inputs(t *testing.T) { arg: struct { Name string Email string `form:"type=email;placeholder=bob@example.com"` + City string `form:"class=custom-city-class"` }{ Name: "Michael Scott", + City: "New York", }, want: template.HTML(strings.Join([]string{ strings.TrimSpace(` `), strings.TrimSpace(` `), + strings.TrimSpace(` + `), }, "")), }, } diff --git a/reflect.go b/reflect.go index d06a337..066f503 100644 --- a/reflect.go +++ b/reflect.go @@ -101,6 +101,9 @@ func applyTags(f *field, tags map[string]string) { // Probably shouldn't be HTML but whatever. f.Footer = template.HTML(v) } + if v, ok := tags["class"]; ok { + f.Class = template.HTMLEscapeString(v) + } } func parseTags(tags string) map[string]string { @@ -134,4 +137,5 @@ type field struct { ID string Value interface{} Footer template.HTML + Class string } diff --git a/reflect_test.go b/reflect_test.go index 1e8c65a..4d6b81e 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -196,6 +196,24 @@ func Test_fields(t *testing.T) { }, }, }, + { + name: "custom css class", + arg: struct { + Name string `form:"class=custom-css-class"` + }{ + Name: "Michael Scott", + }, + want: []field{ + { + Name: "Name", + Label: "Name", + Placeholder: "Name", + Type: "text", + Value: "Michael Scott", + Class: "custom-css-class", + }, + }, + }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) {