view

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 7 Imported by: 0

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

View Source
const ContentType = "text/html; charset=utf-8"

ContentType is the content type of a rendered page.

Variables

This section is empty.

Functions

func CSRFToken

func CSRFToken(ctx context.Context) string

CSRFToken returns the token of this request, or the empty string.

func Error

func Error(ctx context.Context, field string) string

Error returns the message of this field, or the empty string.

if view.HasError(ctx, "title") { <span>{ view.Error(ctx, "title") }</span> }

func Fields

func Fields(ctx context.Context) *router.Fields

Fields returns the validation result of this request, or nil.

func HasError

func HasError(ctx context.Context, field string) bool

HasError reports whether this field failed validation.

func Old

func Old(ctx context.Context, field string) string

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

func RenderContext(c *router.Ctx) context.Context

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

func Status(code int, c Component) router.Response

Status renders a component with this status, for example 404 or 422. The root package names it ViewStatus.

func Toasts

func Toasts(ctx context.Context) []router.Toast

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

func View(c Component) router.Response

View renders a component with 200 and the HTML content type.

return view.View(pages.Index(posts)), nil

func WithCSRFToken

func WithCSRFToken(ctx context.Context, token string) context.Context

WithCSRFToken returns a context that carries the CSRF token of the request.

func WithFields

func WithFields(ctx context.Context, f *router.Fields) context.Context

WithFields returns a context that carries the validation result.

func WithForm

func WithForm(page func(c *router.Ctx, f *router.Fields) Component) router.Option

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.

func WithOld

func WithOld(ctx context.Context, form url.Values) context.Context

WithOld returns a context that carries the submitted form values. View calls it. A test calls it to render a component alone.

func WithToasts

func WithToasts(ctx context.Context, toasts []router.Toast) context.Context

WithToasts returns a context that carries the toasts of the response.

Types

type Component

type Component interface {
	// Render writes the markup of the component to w.
	Render(ctx context.Context, w io.Writer) error
}

Component renders itself. A templ component satisfies it.

func CSRF

func CSRF() Component

CSRF returns the hidden field that an unsafe form must carry. The CSRF middleware reads the field. A form that does not carry it returns 419.

<form method="post">
  @view.CSRF()
</form>

type Func

type Func func(ctx context.Context, w io.Writer) error

Func adapts a function to the Component interface. A test and a small fragment use it. A page uses templ.

func (Func) Render

func (f Func) Render(ctx context.Context, w io.Writer) error

Render calls the function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL