Skip to content
Thanatat Tamtan edited this page Jul 19, 2026 · 3 revisions

hime.Context provides helpers for writing responses. Each returns an error, so you can return it directly from a hime.Handler.

Text and HTML

func handler(ctx *hime.Context) error {
	return ctx.String("Hello, %s", "Hime")
}
func handler(ctx *hime.Context) error {
	return ctx.HTML(`<h1>Hello, Hime</h1>`)
}

JSON and XML

func handler(ctx *hime.Context) error {
	return ctx.JSON(map[string]any{"message": "hello"})
}
func handler(ctx *hime.Context) error {
	return ctx.XML(Greeting{Message: "hello"})
}

Status code

Set the status with ctx.Status (chainable) and read it back with ctx.StatusCode.

func handler(ctx *hime.Context) error {
	return ctx.Status(http.StatusCreated).JSON(data)
}
code := ctx.StatusCode() // status set via Status, defaults to 200

Shortcuts: ctx.NoContent() (204), ctx.NotFound() (404), and ctx.Error(msg) (writes the set status, or 500 when none/under 400).

Templates

Render a named view, a component, or an inline template:

func handler(ctx *hime.Context) error {
	return ctx.View("index", data)
}

View can also render a single {{block}}/{{define}} of a view with ctx.View("index#content", data), and ctx.ViewPartial picks fragment vs full page for htmx requests — see HTMX.

See Template and Component for details.

ETag

Enable ETag globally with app.ETag = true, or per response with ctx.ETag(true). When enabled, View, Component, Render, JSON, XML, and HTML compute a weak ETag and reply 304 Not Modified when the request's If-None-Match matches.

Cache control

ctx.NoCache() (chainable) sets Cache-Control: no-store, opting the response out of browser and shared caches. For cacheable responses that branch on htmx headers, see Caching in HTMX.

Files and raw data

ctx.File("path/to/file.pdf") // serve a file with http.ServeFile
ctx.Bytes(b)                 // write raw bytes
ctx.CopyFrom(reader)         // copy from an io.Reader

Redirects

ctx.Redirect("/login")      // 302 (303 for non-GET requests)
ctx.RedirectTo("home")      // redirect to a named Route
ctx.RedirectBack("/")       // back to the Referer, or the fallback
ctx.SafeRedirect(userInput) // strips the host, defeating open redirects

See Route for named routes.

With app.HTMXAwareRedirect = true, the whole family answers htmx partial requests with HX-Redirect + 204 instead of a 3xx — see HTMX.

Flash messages

Flash messages are shown once on the next request — the post/redirect/get pattern. They are kept in a cookie (no session store) and cleared on read.

func save(ctx *hime.Context) error {
	// ... save ...
	ctx.AddFlash("success", "Saved!")
	return ctx.Redirect("/items")
}

func list(ctx *hime.Context) error {
	return ctx.View("items", map[string]any{"flash": ctx.Flashes()})
}
{{range .flash.success}}<div class="ok">{{.}}</div>{{end}}

Flashes returns the messages keyed by category and clears them; call it before writing the response. Flash messages are not signed — keep them to non-sensitive UI text.

Clone this wiki locally