Documentation
¶
Overview ¶
Package render hosts renderer interfaces and registry helpers, matching the extensibility goals detailed in go-form-gen.md:137-159 and go-form-gen.md:223-239. Concrete renderers live under pkg/renderers.
Index ¶
- Variables
- func ApplySubset(form *model.FormModel, subset FieldSubset)
- func LocalizeFormModel(form *model.FormModel, opts RenderOptions)
- func MergeFormErrors(existing []string, extras ...string) []string
- func MergeHiddenFields(base map[string]string, fields ...HiddenField) map[string]string
- func TemplateI18nFuncs(t Translator, cfg TemplateI18nConfig) map[string]any
- type ChromeClasses
- type ErrorMapping
- type FieldSubset
- type HiddenField
- type MissingTranslationHandler
- type Registry
- type RenderOptions
- type Renderer
- type TemplateI18nConfig
- type Translator
- type ValueWithProvenance
Constants ¶
This section is empty.
Variables ¶
var ErrMissingTranslator = errors.New("render: missing translator")
Functions ¶
func ApplySubset ¶
func ApplySubset(form *model.FormModel, subset FieldSubset)
ApplySubset removes fields that do not match the supplied subset filters. It operates on top-level fields and prunes section metadata so renderers do not render empty sections after filtering. When subset is empty or form is nil, the form is returned unchanged.
func LocalizeFormModel ¶ added in v0.5.0
func LocalizeFormModel(form *model.FormModel, opts RenderOptions)
LocalizeFormModel mutates the supplied form model in place, translating any configured `*Key` hints in the UI schema into their localized string values.
This is best-effort: malformed metadata payloads are ignored and translation failures are routed through opts.OnMissing.
func MergeFormErrors ¶
MergeFormErrors concatenates and normalises multiple form-level error slices, trimming whitespace and removing duplicates while preserving order.
func MergeHiddenFields ¶
func MergeHiddenFields(base map[string]string, fields ...HiddenField) map[string]string
MergeHiddenFields returns a copy of base with the provided fields applied. Empty names are ignored; later fields win on name collisions.
func TemplateI18nFuncs ¶ added in v0.5.0
func TemplateI18nFuncs(t Translator, cfg TemplateI18nConfig) map[string]any
TemplateI18nFuncs returns a map suitable for injecting into go-template engines (e.g. via vanilla.WithTemplateFuncs / preact.WithTemplateFuncs).
The main helper signature is:
translate(localeSrc, key, ...args) string
Where localeSrc can be a string locale (e.g. "en-US") or a map/struct that contains a locale value under cfg.LocaleKey.
Types ¶
type ChromeClasses ¶ added in v0.17.0
type ChromeClasses struct {
// Form overrides the CSS class attribute on the root <form> element.
Form string
// Header overrides the header wrapping title/subtitle chrome.
Header string
// Section overrides the section wrapper used for grouped fields.
Section string
// Fieldset overrides the section wrapper when section.fieldset is true.
Fieldset string
// Actions overrides the action row container class list.
Actions string
// Errors overrides the top-of-form error summary class list.
Errors string
// Grid overrides the grid container used for field layouts.
Grid string
}
ChromeClasses overrides high-level class lists in renderer templates. When a field is non-empty the value replaces the renderer's default classes entirely.
type ErrorMapping ¶
ErrorMapping splits a go-errors compatible payload into field-level and form-level messages keyed by the dotted field paths used throughout the render pipeline.
func MapErrorPayload ¶
func MapErrorPayload(form model.FormModel, payload map[string][]string) ErrorMapping
MapErrorPayload normalises server error payloads (including go-errors style JSON pointer paths) into dotted field identifiers that renderers can consume. Unknown paths are treated as form-level errors so messages are not lost.
type FieldSubset ¶
FieldSubset describes the allowed groups, tags, or sections for partial rendering. When all slices are empty the form is left untouched.
type HiddenField ¶
HiddenField represents a hidden form input emitted alongside the visible schema. Use the helpers (CSRFToken, AuthToken, VersionField) to add common fields without repeating boilerplate.
func AuthToken ¶
func AuthToken(name, token string) HiddenField
AuthToken constructs a hidden field carrying an authentication token or session hint.
func CSRFToken ¶
func CSRFToken(name, token string) HiddenField
CSRFToken constructs a hidden field carrying the provided token. Callers supply the input name to match their backend expectations (for example, "_csrf" or "csrf_token").
func Hidden ¶
func Hidden(name string, value any) HiddenField
Hidden returns a HiddenField for an arbitrary name/value pair.
func SortedHiddenFields ¶
func SortedHiddenFields(fields map[string]string) []HiddenField
SortedHiddenFields normalises and sorts hidden fields for deterministic rendering. Empty names are dropped.
func VersionField ¶
func VersionField(name string, version any) HiddenField
VersionField constructs a hidden field used for optimistic locking or version-aware submissions (for example, "if-match" or "version").
type MissingTranslationHandler ¶ added in v0.5.0
MissingTranslationHandler decides what string should be emitted when a translation cannot be resolved.
Convention: when go-formgen performs model localization it passes a single map argument in args containing { "default": <existing text> }.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry stores renderers by name, providing discovery and duplication safeguards. Implementations can embed or wrap this for dependency injection.
func (*Registry) MustRegister ¶
MustRegister panics on registration failure. Useful for init-time wiring.
type RenderOptions ¶
type RenderOptions struct {
// Method overrides the HTTP method declared by the form model. Renderers are
// responsible for translating unsupported verbs (PATCH/PUT/DELETE) into
// browser-friendly POST submissions plus a hidden _method input when needed.
Method string
// Subset restricts rendering to fields whose group, tags, or section match
// the supplied tokens. Empty subsets leave the form unchanged.
Subset FieldSubset
// Values pre-populates rendered controls using dotted field paths (e.g.
// "author.email"). Values may be wrapped in ValueWithProvenance to attach
// provenance labels or lock fields as readonly/disabled. Renderers can
// decide how to handle nested values or collections for advanced components
// such as chips/typeahead controls.
Values map[string]any
// Errors surfaces server-side validation feedback keyed by field path. The
// vanilla renderer maps these into inline chrome plus data-validation
// attributes so the runtime and assistive tech can reflect the state without
// waiting for client-side validation.
Errors map[string][]string
// FormErrors carries non-field-specific validation messages that should
// render at the top of the form. These often come from request-scoped or
// cross-field validation in the backend.
FormErrors []string
// HiddenFields injects name/value pairs as hidden inputs, useful for CSRF
// tokens, auth/session hints, optimistic locking versions, or other
// submission metadata that should travel with the form without showing up in
// the visible schema.
HiddenFields map[string]string
// Locale selects the locale used by render-time localization helpers.
Locale string
// Translator enables model and template-level localization.
Translator Translator
// OnMissing customizes what string is used when a translation is missing.
OnMissing MissingTranslationHandler
// Theme passes renderer configuration derived from a go-theme Selection so
// renderers can resolve partials, assets, and tokens consistently.
Theme *theme.RendererConfig
// VisibilityContext carries evaluator-specific inputs such as current form
// values or feature flags used to decide whether a field should render.
VisibilityContext visibility.Context
// TopPadding controls how many leading newlines renderers emit before the
// root form markup when no external stylesheets or inline styles are
// present. A zero value allows the renderer to apply its default.
TopPadding int
// OmitAssets instructs renderers to skip emitting <link>, <style>, and
// <script> tags. This is useful for rendering partial forms (e.g., modal
// bodies) that will be embedded in a page where the parent already supplies
// these assets.
OmitAssets bool
// ChromeClasses overrides high-level CSS class lists in renderer templates.
// When nil or empty, renderer defaults are used.
ChromeClasses *ChromeClasses
}
RenderOptions describe per-request data that renderers can use to customise their output without mutating the form model pipeline.
type Renderer ¶
type Renderer interface {
Name() string
ContentType() string
Render(ctx context.Context, model model.FormModel, options RenderOptions) ([]byte, error)
}
Renderer converts a FormModel into a byte representation (HTML, JSX, etc.).
type TemplateI18nConfig ¶ added in v0.5.0
type TemplateI18nConfig struct {
// LocaleKey selects the field/key used to infer locale from template data
// when callers pass a struct or map instead of a raw string.
LocaleKey string
// FuncName customizes the translator helper name (defaults to "translate").
FuncName string
// OnMissing controls the string returned when a translation is missing.
OnMissing MissingTranslationHandler
}
TemplateI18nConfig configures template-level translation helpers.
type Translator ¶ added in v0.5.0
Translator resolves a string for a given locale and message key. It matches github.com/goliatone/go-i18n's Translator interface so downstream projects can pass that implementation without introducing a hard dependency.
type ValueWithProvenance ¶
ValueWithProvenance attaches optional provenance metadata to a prefilled value. Renderers can surface the provenance label and enforce readonly or disabled state when provided.
func PrefillValue ¶
func PrefillValue(value any, provenance string) ValueWithProvenance
PrefillValue is a convenience helper for constructing ValueWithProvenance entries when the caller only needs to specify the value and provenance label.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package template defines renderer-agnostic template interfaces and adapters.
|
Package template defines renderer-agnostic template interfaces and adapters. |