:title
RENDER

:sig
function RENDER()
function RENDER:NAME()

:content
`RENDER` is the HTTP entrypoint of a unit. When a request routes to a `.capy` file, Bearer calls that file's `RENDER` handler.

The handler binds `request` implicitly as a `dval` and returns nothing. Everything it `print`s becomes the response body.

## A minimal page

    function RENDER() {
        print("<h1>Hello</h1>")
    }

Read input from the shared request value with a converter and a fallback. A fallback gives a missing parameter a defined value:

    var name := string(request.query.name, fallback: "guest")

## Response control

`print()` writes the body. Set status, headers, and cookies in `request.out`:

    request.out.status = 404
    request.out.headers["Cache-Control"] = "no-store"
    request.out.cookies["visited"] = "1"

`redirect()` sets the status and `Location` header together. A handler may `return` early at any point; whatever it printed so far is still sent.

## Named handlers

`RENDER:NAME` declares an additional named entrypoint in the same file, selected by the caller rather than by the URL path. A file's unnamed `RENDER` remains the one that plain routing reaches.

## What runs around it

The unit's `ONCE` handler runs before `RENDER` the first time the unit is used in a request. Fragments are pulled in with `component_render()` or `component()`; another unit's exported functions are reached with `unit_load()` or `unit_call()`.

Every Capy unit must export at least one handler — `RENDER`, `CLI`, `COMPONENT`, `WS`, `ONCE`, or `INIT` — or metadata via `#exports`. A file with none fails to compile.

:example capy render
print(string(request.method), "\n")

:example capy render read a query parameter with a fallback
print("<h1>Hello, ", string(request.query.name, fallback: "guest"), "</h1>", "\n")

:example capy render set a status and header before printing
request.out.status = 201
request.out.headers["X-Demo"] = "yes"
print("created\n")

:example capy render return early
if !request.query.id?
{
	request.out.status = 400
	print("id required\n")
	return
}
print("id ", s64(request.query.id), "\n")

:see-group
handlers

:see
type/request
handler/component
component
redirect
