All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
67 lines
1.5 KiB
Markdown
67 lines
1.5 KiB
Markdown
---
|
|
linkTitle: Rendering
|
|
title: Rendering
|
|
weight: 6
|
|
---
|
|
|
|
**go-form** allows you to render a form using Go's built-in template engine.
|
|
Here is a simple example that displays a form:
|
|
|
|
```golang
|
|
myForm := form.NewForm(...)
|
|
|
|
render := theme.NewRenderer(theme.Bootstrap5)
|
|
|
|
tpl, _ := template.New("page").Funcs(render.FuncMap()).Parse(`
|
|
<html>
|
|
<head>
|
|
<title>My form</title>
|
|
</head>
|
|
<body>
|
|
{{ form .Form }}
|
|
</body>
|
|
</html>
|
|
`)
|
|
|
|
b := new(strings.Builder)
|
|
|
|
tpl.Execute(w, map[string]any{
|
|
"Form": myForm,
|
|
})
|
|
|
|
fmt.Println(b.String())
|
|
```
|
|
|
|
{{% goplay-auto-import-main %}}
|
|
<pre class="hidden">
|
|
@import "fmt"
|
|
@import "html/template"
|
|
@import "strings"
|
|
@import
|
|
@import "github.com/yosssi/gohtml"
|
|
@import "gitnet.fr/deblan/go-form/example"
|
|
@import "gitnet.fr/deblan/go-form/theme"
|
|
|
|
form := example.CreateDataForm()
|
|
render := theme.NewRenderer(theme.Html5)
|
|
// render := theme.NewRenderer(theme.Bootstrap5)
|
|
|
|
tpl, _ := template.New("page").Funcs(render.FuncMap()).Parse(`{{ form .Form }}`)
|
|
|
|
buff := new(strings.Builder)
|
|
|
|
tpl.Execute(buff, map[string]any{
|
|
"Form": form,
|
|
})
|
|
|
|
fmt.Println(gohtml.Format(buff.String()))
|
|
</pre>
|
|
{{% /goplay-auto-import-main %}}
|
|
|
|
Other helper functions are available to render specific parts of the form:
|
|
|
|
- `form_errors`: displays the form's global errors
|
|
- `form_row` : renders the label, errors, and widget of a field
|
|
- `form_label`: renders only the label of a field
|
|
- `form_widget`: renders only the widget of a field
|
|
- `form_widget_errors`: renders only the errors of a specific field
|