package forms import ( "bytes" _ "embed" "html/template" ) // The script and styles every form uses. The build publishes them with // fingerprinted names when a site has forms. var ( //go:embed static/hotdog-forms.js Script []byte //go:embed static/hotdog-forms.css Style []byte ) var formTpl = template.Must(template.New("form").Parse(`
`)) // MaxLen is the field's length limit, for the maxlength attribute. func (f Field) MaxLen() int { return f.maxLen() } // Autocomplete is the browser autofill hint for the field, if one fits. func (f Field) Autocomplete() string { switch f.Type { case "email": return "email" case "tel": return "tel" case "url": return "url" } if f.Name == "name" { return "name" } return "" } // Render returns the form's HTML. js and css are the published addresses of // the forms script and stylesheet. func (f *Form) Render(js, css string) (template.HTML, error) { var b bytes.Buffer if err := formTpl.Execute(&b, map[string]any{"F": f, "JS": js, "CSS": css}); err != nil { return "", err } // Safe: produced by html/template, which escaped every value from the YAML. return template.HTML(b.String()), nil }