Rendering
Rendering
go-form allows you to render a form using Go’s built-in template engine. Here is a simple example that displays a form:
myForm := form.NewForm(...)
render := theme.NewRenderer(theme.Html5)
tpl, _ := template.New("page").Funcs(render.FuncMap()).Parse(`
<html>
<head>
<title>My form</title>
</head>
<body>
{{ form .Form }}
</body>
</html>
`)
tpl.Execute(w, map[string]any{
"Form": myForm,
})
tpl, _ := template.New("example").Funcs(render.FuncMap()).Parse("{{ form_widget .Form }}")
b := new(strings.Builder)
tpl.Execute(b, map[string]any{"Form": f})
fmt.Println(b.String())Other helper functions are available to render specific parts of the form:
form_errors: displays the form’s global errorsform_row: renders the label, errors, and widget of a fieldform_label: renders only the label of a fieldform_widget: renders only the widget of a fieldform_widget_errors: renders only the errors of a specific field