Capy-Bearer Documentation

RENDER

Capy

function RENDER()
function RENDER(request : any)
function RENDER:NAME()
function RENDER:NAME(request : any)

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 an any and returns nothing. You can also declare request : any explicitly. Everything it prints becomes the response body.

A minimal page

function RENDER()
{
	var name := string(request.query.name, fallback: "guest")
	print(<><h1>Hello, <?= name ?></h1></>)
}

Output

<h1>Hello, guest</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")

Output

No output

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"

Output

HTTP 404

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.

Use unit_call() with the full handler name. This complete unit dispatches to its own named handler:

function RENDER()
{
	unit_call(":RENDER:DETAIL")
}

function RENDER:DETAIL(request : any)
{
	print(request.method)
}

The call writes output directly and returns none. It does not return the handler's printed text.

What runs around it

The unit's INIT handler runs when Bearer loads the unit. Its ONCE handler runs before the first render or component invocation in the 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 expose a handler or exported metadata. A data-only unit can use #exports without defining RENDER. A file with neither fails to compile.

print(request.method, "\n")

Output

GET
print(<><h1>Hello, <?= string(request.query.name, fallback: "guest") ?></h1></>, "\n")

Output

<h1>Hello, guest</h1>
request.out.status = 201
request.out.headers["X-Demo"] = "yes"
print("created\n")

Output

HTTP 201
created
if !request.query.id?
{
	request.out.status = 400
	print("id required\n")
	return
}
print("id ", s64(request.query.id), "\n")

Output

HTTP 400
id required