Documentation
¶
Overview ¶
Package view provides deterministic, bounded server-side HTML template parsing and atomic HTTP rendering.
Index ¶
- Variables
- func AcceptsHTML(header string) bool
- type Options
- type Renderer
- func (renderer *Renderer) Render(ctx context.Context, writer http.ResponseWriter, name string, status int, ...) error
- func (renderer *Renderer) Respond(ctx context.Context, writer http.ResponseWriter, result Result) error
- func (renderer *Renderer) TemplateNames() []string
- func (renderer *Renderer) WriteError(writer http.ResponseWriter, request *http.Request, err error, ...) error
- type Result
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrOutputLimit = errors.New("template output limit exceeded")
ErrOutputLimit means template execution exceeded the configured response bound.
Functions ¶
func AcceptsHTML ¶
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 ¶
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 RenderStatus ¶
RenderStatus constructs a template result with an explicit body-capable success status.