view

package
v0.1.0-preview.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package view provides deterministic, bounded server-side HTML template parsing and atomic HTTP rendering.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrOutputLimit = errors.New("template output limit exceeded")

ErrOutputLimit means template execution exceeded the configured response bound.

Functions

func AcceptsHTML

func AcceptsHTML(header string) bool

AcceptsHTML reports whether an Accept header permits an HTML representation. An empty header accepts HTML.

Types

type Options

type Options struct {
	MaxOutputBytes int
	// ErrorTemplate enables HTML problem rendering through WriteError.
	ErrorTemplate string
	// ErrorModel converts a safe problem and request into template data.
	ErrorModel func(*http.Request, web.Problem) any
}

Options configures an immutable renderer.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer is an immutable parsed HTML template set. It is safe for concurrent execution when caller-provided template functions are also safe.

Example
renderer, err := Parse(
	fstest.MapFS{
		"welcome.html": {Data: []byte(`<h1>Hello, {{.Name}}</h1>`)},
	},
	[]string{"*.html"},
	nil,
	Options{},
)
if err != nil {
	panic(err)
}
recorder := httptest.NewRecorder()
if err := renderer.Render(
	context.Background(),
	recorder,
	"welcome.html",
	http.StatusOK,
	struct{ Name string }{Name: "Spice"},
); err != nil {
	panic(err)
}
fmt.Println(recorder.Body.String())
Output:
<h1>Hello, Spice</h1>

func Parse

func Parse(
	source fs.FS,
	patterns []string,
	functions template.FuncMap,
	options Options,
) (*Renderer, error)

Parse expands patterns against source, reads matched regular files within a fixed aggregate bound, and parses them in lexical path order. Construction performs no process-environment or network access.

func (*Renderer) Render

func (renderer *Renderer) Render(
	ctx context.Context,
	writer http.ResponseWriter,
	name string,
	status int,
	data any,
) error

Render executes name into a bounded private buffer, then writes one HTML response only after execution succeeds. Status must permit a response body.

func (*Renderer) Respond

func (renderer *Renderer) Respond(
	ctx context.Context,
	writer http.ResponseWriter,
	result Result,
) error

Respond validates and writes a view result atomically where possible.

func (*Renderer) TemplateNames

func (renderer *Renderer) TemplateNames() []string

TemplateNames returns the sorted exact executable names discovered during parsing.

func (*Renderer) WriteError

func (renderer *Renderer) WriteError(
	writer http.ResponseWriter,
	request *http.Request,
	err error,
	mapper web.ErrorMapper,
) error

WriteError maps an application error to a safe problem and renders the configured HTML error template. Renderers without an error template retain the standard RFC 9457 JSON behavior.

type Result

type Result struct {
	// contains filtered or unexported fields
}

Result is one validated server-rendered controller outcome.

func Render

func Render[T any](name string, model T) (Result, error)

Render constructs an HTTP 200 template result.

func RenderStatus

func RenderStatus[T any](
	status int,
	name string,
	model T,
) (Result, error)

RenderStatus constructs a template result with an explicit body-capable success status.

func SeeOther

func SeeOther(location string) (Result, error)

SeeOther constructs a safe local HTTP 303 redirect.

func (Result) Validate

func (result Result) Validate() error

Validate checks the closed result contract.

Jump to

Keyboard shortcuts

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