render

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentDisposition = "Content-Disposition"
)

Response disposition values used in Content-Disposition headers.

View Source
const ContentType = httpctype.Key

ContentType is a re-export of httpctype.Key kept for ergonomics.

Variables

View Source
var (
	// PrettyIndent indent string for render JSON or XML
	PrettyIndent = "  "

	// FallbackType for auto response
	FallbackType = httpctype.MIMEText
)

Functions

func Auto

func Auto(w http.ResponseWriter, r *http.Request, obj any) (err error)

Auto render data to response

func AutoStatus

func AutoStatus(w http.ResponseWriter, req *http.Request, data any, tplName ...string) error

AutoStatus picks an output format based on Accept (see Responder.Auto).

func BinaryStatus

func BinaryStatus(w http.ResponseWriter, status int, in io.Reader, outName string, inline bool) error

BinaryStatus streams in as an attachment (or inline).

func Blob

func Blob(w http.ResponseWriter, contentType string, data []byte) (err error)

Blob writes out []byte

func ContentStatus

func ContentStatus(w http.ResponseWriter, status int, body []byte, contentType string) error

ContentStatus writes raw bytes with the given Content-Type and status.

func EmptyStatus

func EmptyStatus(w http.ResponseWriter) error

EmptyStatus writes 204 No Content via the default Responder.

func HTML

func HTML(w http.ResponseWriter, data string) error

HTML writes out as html text. if data is empty, only write headers

func HTMLBytes

func HTMLBytes(w http.ResponseWriter, data []byte) error

HTMLBytes writes out as html text. if data is empty, only write headers

func HTMLStatus

func HTMLStatus(w http.ResponseWriter, status int, name string, data any, layout ...string) error

HTMLStatus renders a template via the default Responder's TemplateRenderer.

func HTMLStringStatus

func HTMLStringStatus(w http.ResponseWriter, status int, tplContent string, data any) error

HTMLStringStatus parses and executes an inline template.

func HTMLTextStatus

func HTMLTextStatus(w http.ResponseWriter, status int, html string) error

HTMLTextStatus writes html as the response body without templating.

func Init

func Init(fns ...OptionFn)

Init applies OptionFns to the default Responder. Safe to call before first use; not safe to interleave with concurrent traffic.

func JSON

func JSON(w http.ResponseWriter, obj any) error

JSON response rendering

func JSONIndented

func JSONIndented(w http.ResponseWriter, obj any) error

JSONIndented response rendering with indent

func JSONP

func JSONP(callback string, obj any, w http.ResponseWriter) error

JSONP response rendering

func JSONPStatus

func JSONPStatus(w http.ResponseWriter, status int, callback string, v any) error

JSONPStatus wraps the JSON-encoded v in a JS callback invocation.

func JSONStatus

func JSONStatus(w http.ResponseWriter, status int, v any) error

JSONStatus encodes v as JSON via the default Responder.

func LoadTemplateFiles

func LoadTemplateFiles(files ...string) error

LoadTemplateFiles on the default Responder.

func LoadTemplateGlob

func LoadTemplateGlob(pattern string) error

LoadTemplateGlob on the default Responder.

func Plain

func Plain(w http.ResponseWriter, str string) error

Plain writes out a string as plain text. alias of the Text()

func SetTemplateRenderer

func SetTemplateRenderer(t TemplateRenderer)

SetTemplateRenderer on the default Responder.

func Text

func Text(w http.ResponseWriter, str string) error

Text writes out a string as plain text.

func TextBytes

func TextBytes(w http.ResponseWriter, data []byte) error

TextBytes writes out a string as plain text.

func TextStatus

func TextStatus(w http.ResponseWriter, status int, v string) error

TextStatus writes a text response with the given status.

func XML

func XML(w http.ResponseWriter, obj any) error

XML response rendering

func XMLPretty

func XMLPretty(w http.ResponseWriter, obj any) error

XMLPretty response rendering with indent

func XMLStatus

func XMLStatus(w http.ResponseWriter, status int, v any) error

XMLStatus encodes v as XML via the default Responder.

Types

type JSONPRenderer

type JSONPRenderer struct {
	Callback string
}

JSONPRenderer for response JSONP content to client

func (JSONPRenderer) Render

func (r JSONPRenderer) Render(w http.ResponseWriter, obj any) (err error)

Render JSONP to client

type JSONRenderer

type JSONRenderer struct {
	// Data any
	// Indent string for encode
	Indent string
	// NotEscape HTML string
	NotEscape bool
}

JSONRenderer for response JSON content to client

func NewJSONIndented

func NewJSONIndented() JSONRenderer

NewJSONIndented instance

func (JSONRenderer) Render

func (r JSONRenderer) Render(w http.ResponseWriter, obj any) (err error)

Render JSON to client

type OptionFn

type OptionFn func(*Options)

OptionFn mutates Options during New / Init.

type Options

type Options struct {
	// JSONIndent indents JSON output using PrettyIndent.
	JSONIndent bool
	// JSONPrefix is written before each JSON body. Useful for anti-hijack
	// payloads like ")]}',\n".
	JSONPrefix string

	// XMLIndent indents XML output using PrettyIndent.
	XMLIndent bool
	// XMLPrefix is written before each XML body. Defaults to xml.Header.
	XMLPrefix string

	// Charset appended to text-like content types when AddCharset is true.
	Charset string
	// AddCharset toggles charset suffixing on text-like content types.
	AddCharset bool

	// ContentType is the fallback used by Auto when the request supplies
	// neither Accept nor a response Content-Type.
	ContentType string

	// Per-format content types. Defaults to httpctype MIME constants
	// (no charset); the charset is appended at write time when AddCharset
	// is true. Override for custom MIME variants.
	ContentBinary string
	ContentHTML   string
	ContentXML    string
	ContentText   string
	ContentJSON   string
	ContentJSONP  string
}

Options configure a Responder. All fields have working defaults — see New for the values.

type Renderer

type Renderer interface {
	Render(w http.ResponseWriter, obj any) error
}

Renderer interface

type RendererFunc

type RendererFunc func(w http.ResponseWriter, obj any) error

RendererFunc definition

func (RendererFunc) Render

func (fn RendererFunc) Render(w http.ResponseWriter, obj any) error

Render to http.ResponseWriter

type Responder

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

Responder writes HTTP responses in common formats. It is safe for concurrent use after construction; do not mutate Options after New.

HTML rendering is delegated to a pluggable TemplateRenderer, set via SetTemplateRenderer — Responder itself imports no template engine.

func Default

func Default() *Responder

Default returns the package-level Responder so callers can tweak it directly (e.g. render.Default().SetTemplateRenderer(...)).

func New

func New(fns ...OptionFn) *Responder

New returns a Responder with sensible defaults; pass OptionFn to override.

func (*Responder) Auto

func (r *Responder) Auto(w http.ResponseWriter, req *http.Request, data any, tplName ...string) error

Auto picks an output format based on the request Accept header. HTML responses require a non-empty tplName and a configured TemplateRenderer. Returns an error when none of the accepted types are supported.

func (*Responder) Binary

func (r *Responder) Binary(w http.ResponseWriter, status int, in io.Reader, outName string, inline bool) error

Binary streams in as an attachment (or inline when inline=true) with the given filename. Uses io.Copy so memory usage is bounded.

func (*Responder) Content

func (r *Responder) Content(w http.ResponseWriter, status int, body []byte, contentType string) error

Content writes the given bytes with the given Content-Type and status.

func (*Responder) Data

func (r *Responder) Data(w http.ResponseWriter, status int, body []byte, contentType string) error

Data is an alias for Content — kept for API symmetry with respond.

func (*Responder) Empty

func (r *Responder) Empty(w http.ResponseWriter) error

Empty is an alias for NoContent.

func (*Responder) HTML

func (r *Responder) HTML(w http.ResponseWriter, status int, name string, data any, layout ...string) error

HTML renders the named template via the configured TemplateRenderer. Returns an error if no engine has been installed.

func (*Responder) HTMLString

func (r *Responder) HTMLString(w http.ResponseWriter, status int, tplContent string, data any) error

HTMLString parses and executes an inline template using std html/template. No external engine needed.

func (*Responder) HTMLText

func (r *Responder) HTMLText(w http.ResponseWriter, status int, html string) error

HTMLText writes html as the response body without any templating.

func (*Responder) JSON

func (r *Responder) JSON(w http.ResponseWriter, status int, v any) error

JSON encodes v as JSON and writes the response.

func (*Responder) JSONP

func (r *Responder) JSONP(w http.ResponseWriter, status int, callback string, v any) error

JSONP wraps the JSON-encoded v in a JavaScript callback invocation.

func (*Responder) LoadTemplateFiles

func (r *Responder) LoadTemplateFiles(files ...string) error

LoadTemplateFiles forwards to the configured TemplateRenderer if it implements TemplateLoader.

func (*Responder) LoadTemplateGlob

func (r *Responder) LoadTemplateGlob(pattern string) error

LoadTemplateGlob forwards to the configured TemplateRenderer if it implements TemplateLoader.

func (*Responder) NoContent

func (r *Responder) NoContent(w http.ResponseWriter) error

NoContent writes 204 with no body.

func (*Responder) Options

func (r *Responder) Options() Options

Options returns a copy of the current configuration (read-only).

func (*Responder) SetTemplateRenderer

func (r *Responder) SetTemplateRenderer(t TemplateRenderer)

SetTemplateRenderer installs the engine used by HTML(). Passing nil disables HTML(name, data) — HTMLString and HTMLText remain usable.

func (*Responder) String

func (r *Responder) String(w http.ResponseWriter, status int, v string) error

String is an alias for Text.

func (*Responder) TemplateRenderer

func (r *Responder) TemplateRenderer() TemplateRenderer

TemplateRenderer returns the currently configured engine (or nil).

func (*Responder) Text

func (r *Responder) Text(w http.ResponseWriter, status int, v string) error

Text writes a plain-text response.

func (*Responder) XML

func (r *Responder) XML(w http.ResponseWriter, status int, v any) error

XML encodes v as XML and writes the response, optionally prefixed with XMLPrefix (defaults to xml.Header).

type TemplateLoader

type TemplateLoader interface {
	LoadGlob(pattern string) error
	LoadFiles(files ...string) error
}

TemplateLoader is an optional capability — when the configured TemplateRenderer also implements TemplateLoader, callers can ask Responder to load templates via LoadGlob / LoadFiles. Engines without a load step can omit this interface.

type TemplateRenderer

type TemplateRenderer interface {
	// Render writes the named template to w with the given data and
	// optional layout. Engines that don't support layouts may ignore
	// the layout argument.
	Render(w io.Writer, name string, data any, layout ...string) error
}

TemplateRenderer is the abstract interface a HTML template engine must satisfy to be plugged into a Responder. Keeping it minimal means any engine (easytpl, std html/template, pongo2, …) can be adapted with a tiny wrapper, and rux itself stays template-engine-free.

r := render.New()
r.SetTemplateRenderer(myEngineAdapter)
r.HTML(w, 200, "home.tpl", data)

type XMLRenderer

type XMLRenderer struct {
	// Data any
	Indent string
}

XMLRenderer for response XML content to client

func (XMLRenderer) Render

func (r XMLRenderer) Render(w http.ResponseWriter, obj any) error

Render XML to client

Jump to

Keyboard shortcuts

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