Documentation
¶
Overview ¶
Package view holds the helpers that a server rendered page needs, S10.
The ui package of an application belongs to the scaffold, not to Avero. Avero provides only the helpers that a component calls: the old input, the field errors, the toasts, the CSRF field, and the Response that renders a component.
The package holds no template engine and no dependency on one. Component carries the method set of templ.Component, so a templ component satisfies it with no adapter and no import. See the SDD, S10.
Index ¶
- Constants
- func CSRFToken(ctx context.Context) string
- func Error(ctx context.Context, field string) string
- func Fields(ctx context.Context) *router.Fields
- func HasError(ctx context.Context, field string) bool
- func Old(ctx context.Context, field string) string
- func RenderContext(c *router.Ctx) context.Context
- func Status(code int, c Component) router.Response
- func Toasts(ctx context.Context) []router.Toast
- func View(c Component) router.Response
- func WithCSRFToken(ctx context.Context, token string) context.Context
- func WithFields(ctx context.Context, f *router.Fields) context.Context
- func WithForm(page func(c *router.Ctx, f *router.Fields) Component) router.Option
- func WithOld(ctx context.Context, form url.Values) context.Context
- func WithToasts(ctx context.Context, toasts []router.Toast) context.Context
- type Component
- type Func
Constants ¶
const ContentType = "text/html; charset=utf-8"
ContentType is the content type of a rendered page.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
Error returns the message of this field, or the empty string.
if view.HasError(ctx, "title") { <span>{ view.Error(ctx, "title") }</span> }
func Old ¶
Old returns the value that the person submitted for this field. It returns the empty string for a request that submitted no form, so a first render needs no test.
<input name="title" value={ view.Old(ctx, "title") }>
func RenderContext ¶
RenderContext returns the context that a component reads. It carries the toasts of the response, the CSRF token, the submitted form and the validation result.
A component therefore needs no Ctx, and a test can build the same context with WithOld, WithToasts, WithCSRFToken and WithFields.
func Status ¶
Status renders a component with this status, for example 404 or 422. The root package names it ViewStatus.
func Toasts ¶
Toasts returns the messages of this response, in the order that the handler added them. The flash middleware carries a toast across a redirect, so a message that a handler set before a redirect appears on the next page.
func View ¶
View renders a component with 200 and the HTML content type.
return view.View(pages.Index(posts)), nil
func WithCSRFToken ¶
WithCSRFToken returns a context that carries the CSRF token of the request.
func WithFields ¶
WithFields returns a context that carries the validation result.
func WithForm ¶
WithForm renders the form again when validation fails.
The router calls page with the validation result. The answer carries 422, and the render context holds the field errors and the submitted values, so the component reads them with Error, HasError and Old.
r := router.New(view.WithForm(func(c *router.Ctx, f *router.Fields) view.Component {
return pages.NewPost()
}))
An application that answers JSON needs no option. The default answer is 422 with a map of field name to message.