KScript docs
EN

KScript.Http

Routes, controllers and middleware: <KScript.Routes> files served by kscript serve.

Import it at the top of a script:

<import package="KScript.Http" />
import KScript.Http

A file whose root element is <KScript.Route> imports it for you.

A .kscript file names a package only in its import: write Route { }, never KScript.Route { }.

KScript.Http serves web routes, so its examples can't run inside the docs. Copy one into a .ks.xml file and run it with kscript.

ElementsStatements

#<Any>Any

A route for any method requests to a path. It is answered by a controller (to), a view, a redirect, or the statements inside it (<return value="..." />).

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
path requireda stringThe path, with {name} parameters: /people/{id}. {name?} is optional and {name*} takes the rest of the path.
toa function: @nameThe controller: a <fn> (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.The controller: a fn (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.
viewa stringAnswer with this view instead, from the views folder, with the path parameters as variables.
redirecta stringAnswer with a redirect to this address instead.
statusexpressionThe redirect's status code, 302 by default.
namea stringA name for the route, for @route('name', ...).
label_keya stringA translation key for the route's display label.
dataexpressionOptional route metadata made from a declared record type.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for this route, comma separated.
cana stringAn ability the model's policy must allow before the controller runs: can: update. It is asked about the record the controller's typed path parameter names, fn update(post: Post). A no answers 403, and a guest the ability turns away is sent to sign in (401 for an API).
wherea stringRules for the parameters: where: { id: int, slug: slug } (XML: where="id: int, slug: slug"). Rules: number, int, alpha, alnum, slug, uuid or a regular expression.
etagtrue or falsetrue tags GET answers with an ETag (a hash of the body) and answers If-None-Match with 304 Not Modified when the client already has that version.
cachea stringThe Cache-Control header for GET answers: public, max-age=300, or just a number of seconds (300 is public for 5 minutes), private, no-store...
Example
<KScript.Routes>
  <Any path="/people/{id}" to="people.show" name="people.show" where="id: int" />
</KScript.Routes>
import KScript.Http

Routes {
  Any(path: "/people/{id}", to: @people.show, name: 'people.show', where: 'id: int')
}

#<Delete>Delete

A route for DELETE requests to a path. It is answered by a controller (to), a view, a redirect, or the statements inside it (<return value="..." />).

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
path requireda stringThe path, with {name} parameters: /people/{id}. {name?} is optional and {name*} takes the rest of the path.
toa function: @nameThe controller: a <fn> (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.The controller: a fn (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.
viewa stringAnswer with this view instead, from the views folder, with the path parameters as variables.
redirecta stringAnswer with a redirect to this address instead.
statusexpressionThe redirect's status code, 302 by default.
namea stringA name for the route, for @route('name', ...).
label_keya stringA translation key for the route's display label.
dataexpressionOptional route metadata made from a declared record type.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for this route, comma separated.
cana stringAn ability the model's policy must allow before the controller runs: can: update. It is asked about the record the controller's typed path parameter names, fn update(post: Post). A no answers 403, and a guest the ability turns away is sent to sign in (401 for an API).
wherea stringRules for the parameters: where: { id: int, slug: slug } (XML: where="id: int, slug: slug"). Rules: number, int, alpha, alnum, slug, uuid or a regular expression.
etagtrue or falsetrue tags GET answers with an ETag (a hash of the body) and answers If-None-Match with 304 Not Modified when the client already has that version.
cachea stringThe Cache-Control header for GET answers: public, max-age=300, or just a number of seconds (300 is public for 5 minutes), private, no-store...
Example
<KScript.Routes>
  <Delete path="/people/{id}" to="people.destroy" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Delete(path: "/people/{id}", to: @people.destroy, middleware: auth)
}

#<Get>Get

A route for GET requests to a path. It is answered by a controller (to), a view, a redirect, or the statements inside it (<return value="..." />).

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
path requireda stringThe path, with {name} parameters: /people/{id}. {name?} is optional and {name*} takes the rest of the path.
toa function: @nameThe controller: a <fn> (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.The controller: a fn (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.
viewa stringAnswer with this view instead, from the views folder, with the path parameters as variables.
redirecta stringAnswer with a redirect to this address instead.
statusexpressionThe redirect's status code, 302 by default.
namea stringA name for the route, for @route('name', ...).
label_keya stringA translation key for the route's display label.
dataexpressionOptional route metadata made from a declared record type.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for this route, comma separated.
cana stringAn ability the model's policy must allow before the controller runs: can: update. It is asked about the record the controller's typed path parameter names, fn update(post: Post). A no answers 403, and a guest the ability turns away is sent to sign in (401 for an API).
wherea stringRules for the parameters: where: { id: int, slug: slug } (XML: where="id: int, slug: slug"). Rules: number, int, alpha, alnum, slug, uuid or a regular expression.
etagtrue or falsetrue tags GET answers with an ETag (a hash of the body) and answers If-None-Match with 304 Not Modified when the client already has that version.
cachea stringThe Cache-Control header for GET answers: public, max-age=300, or just a number of seconds (300 is public for 5 minutes), private, no-store...
Example
<KScript.Routes>
  <Get path="/people/{id}" to="people.show" name="people.show" where="id: int" />
</KScript.Routes>
import KScript.Http

Routes {
  Get(path: "/people/{id}", to: @people.show, name: 'people.show', where: 'id: int')
}

#<Group>Group

Routes that share a path prefix, middleware or a name prefix.

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
prefixa stringA path in front of every route inside, such as /admin.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for every route inside, comma separated.
namea stringPut in front of every route name inside, such as admin.
Example
<KScript.Routes>
  <Group prefix="/admin" middleware="auth" name="admin.">
    <Get path="/" to="admin.dashboard" name="home" />
  </Group>
</KScript.Routes>
import KScript.Http

Routes {
  Group(prefix: '/admin', middleware: auth, name: 'admin.') {
    Get(path: '/', to: @admin.dashboard, name: 'home')
  }
}

#<Middleware>Middleware

Middleware for the routes that follow it in this file or group.

Self-closing.A statement on its own. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
use requiredlog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseBuilt-ins or custom functions to run before the routes that follow.
Example
<KScript.Routes>
  <Middleware use="log, cors" />
</KScript.Routes>
import KScript.Http

Routes {
  Middleware(use: [log, cors])
}

#<ModelUpdate>ModelUpdate

Lets a named HTML model form edit selected fields of a record owned by the signed-in user.

Self-closing.A statement on its own. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
name requireda stringThe binding name used by <form model-update>.
model requireda stringThe model in the project's models folder.
owner requireda stringThe column holding the authenticated user's ID.
fields requireda list of names: [a, b]Writable model fields, for example [title, body].
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseAdditional authorization or other middleware for this binding.
Example
<KScript.Routes><ModelUpdate name="edit_note" model="Note" owner="user_id" fields="title, body" /></KScript.Routes>
import KScript.Http

Routes {
  ModelUpdate(name: edit_note, model: Note, owner: user_id, fields: [title, body])
}

#<Options>Options

A route for OPTIONS requests to a path. It is answered by a controller (to), a view, a redirect, or the statements inside it (<return value="..." />).

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
path requireda stringThe path, with {name} parameters: /people/{id}. {name?} is optional and {name*} takes the rest of the path.
toa function: @nameThe controller: a <fn> (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.The controller: a fn (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.
viewa stringAnswer with this view instead, from the views folder, with the path parameters as variables.
redirecta stringAnswer with a redirect to this address instead.
statusexpressionThe redirect's status code, 302 by default.
namea stringA name for the route, for @route('name', ...).
label_keya stringA translation key for the route's display label.
dataexpressionOptional route metadata made from a declared record type.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for this route, comma separated.
cana stringAn ability the model's policy must allow before the controller runs: can: update. It is asked about the record the controller's typed path parameter names, fn update(post: Post). A no answers 403, and a guest the ability turns away is sent to sign in (401 for an API).
wherea stringRules for the parameters: where: { id: int, slug: slug } (XML: where="id: int, slug: slug"). Rules: number, int, alpha, alnum, slug, uuid or a regular expression.
etagtrue or falsetrue tags GET answers with an ETag (a hash of the body) and answers If-None-Match with 304 Not Modified when the client already has that version.
cachea stringThe Cache-Control header for GET answers: public, max-age=300, or just a number of seconds (300 is public for 5 minutes), private, no-store...
Example
<KScript.Routes>
  <Options path="/people/{id}" to="people.update" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Options(path: "/people/{id}", to: @people.update, middleware: auth)
}

#<Patch>Patch

A route for PATCH requests to a path. It is answered by a controller (to), a view, a redirect, or the statements inside it (<return value="..." />).

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
path requireda stringThe path, with {name} parameters: /people/{id}. {name?} is optional and {name*} takes the rest of the path.
toa function: @nameThe controller: a <fn> (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.The controller: a fn (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.
viewa stringAnswer with this view instead, from the views folder, with the path parameters as variables.
redirecta stringAnswer with a redirect to this address instead.
statusexpressionThe redirect's status code, 302 by default.
namea stringA name for the route, for @route('name', ...).
label_keya stringA translation key for the route's display label.
dataexpressionOptional route metadata made from a declared record type.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for this route, comma separated.
cana stringAn ability the model's policy must allow before the controller runs: can: update. It is asked about the record the controller's typed path parameter names, fn update(post: Post). A no answers 403, and a guest the ability turns away is sent to sign in (401 for an API).
wherea stringRules for the parameters: where: { id: int, slug: slug } (XML: where="id: int, slug: slug"). Rules: number, int, alpha, alnum, slug, uuid or a regular expression.
etagtrue or falsetrue tags GET answers with an ETag (a hash of the body) and answers If-None-Match with 304 Not Modified when the client already has that version.
cachea stringThe Cache-Control header for GET answers: public, max-age=300, or just a number of seconds (300 is public for 5 minutes), private, no-store...
Example
<KScript.Routes>
  <Patch path="/people/{id}" to="people.update" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Patch(path: "/people/{id}", to: @people.update, middleware: auth)
}

#<Post>Post

A route for POST requests to a path. It is answered by a controller (to), a view, a redirect, or the statements inside it (<return value="..." />).

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
path requireda stringThe path, with {name} parameters: /people/{id}. {name?} is optional and {name*} takes the rest of the path.
toa function: @nameThe controller: a <fn> (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.The controller: a fn (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.
viewa stringAnswer with this view instead, from the views folder, with the path parameters as variables.
redirecta stringAnswer with a redirect to this address instead.
statusexpressionThe redirect's status code, 302 by default.
namea stringA name for the route, for @route('name', ...).
label_keya stringA translation key for the route's display label.
dataexpressionOptional route metadata made from a declared record type.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for this route, comma separated.
cana stringAn ability the model's policy must allow before the controller runs: can: update. It is asked about the record the controller's typed path parameter names, fn update(post: Post). A no answers 403, and a guest the ability turns away is sent to sign in (401 for an API).
wherea stringRules for the parameters: where: { id: int, slug: slug } (XML: where="id: int, slug: slug"). Rules: number, int, alpha, alnum, slug, uuid or a regular expression.
etagtrue or falsetrue tags GET answers with an ETag (a hash of the body) and answers If-None-Match with 304 Not Modified when the client already has that version.
cachea stringThe Cache-Control header for GET answers: public, max-age=300, or just a number of seconds (300 is public for 5 minutes), private, no-store...
Example
<KScript.Routes>
  <Post path="/people/{id}" to="people.store" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Post(path: "/people/{id}", to: @people.store, middleware: auth)
}

#<Put>Put

A route for PUT requests to a path. It is answered by a controller (to), a view, a redirect, or the statements inside it (<return value="..." />).

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
path requireda stringThe path, with {name} parameters: /people/{id}. {name?} is optional and {name*} takes the rest of the path.
toa function: @nameThe controller: a <fn> (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.The controller: a fn (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.
viewa stringAnswer with this view instead, from the views folder, with the path parameters as variables.
redirecta stringAnswer with a redirect to this address instead.
statusexpressionThe redirect's status code, 302 by default.
namea stringA name for the route, for @route('name', ...).
label_keya stringA translation key for the route's display label.
dataexpressionOptional route metadata made from a declared record type.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for this route, comma separated.
cana stringAn ability the model's policy must allow before the controller runs: can: update. It is asked about the record the controller's typed path parameter names, fn update(post: Post). A no answers 403, and a guest the ability turns away is sent to sign in (401 for an API).
wherea stringRules for the parameters: where: { id: int, slug: slug } (XML: where="id: int, slug: slug"). Rules: number, int, alpha, alnum, slug, uuid or a regular expression.
etagtrue or falsetrue tags GET answers with an ETag (a hash of the body) and answers If-None-Match with 304 Not Modified when the client already has that version.
cachea stringThe Cache-Control header for GET answers: public, max-age=300, or just a number of seconds (300 is public for 5 minutes), private, no-store...
Example
<KScript.Routes>
  <Put path="/people/{id}" to="people.update" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Put(path: "/people/{id}", to: @people.update, middleware: auth)
}

#<Resource>Resource

The usual routes of a JSON resource, answered by the functions a controller script exports: index (GET path), store (POST path), show (GET path/{id}), update (PUT and PATCH path/{id}) and destroy (DELETE path/{id}). Functions the controller doesn't have get no route.

Self-closing.A statement on its own. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
path requireda stringThe resource's path, such as /people.
to requireda stringThe controller: the name a script is imported as (<import path="..." as="people" />).
onlyindex | store | show | update | destroyOnly these actions: [index, show].
exceptindex | store | show | update | destroyAll actions but these: [destroy].
namea stringThe route name prefix (people gives people.index, people.show...). The default is the controller's name.
parama stringThe name of the path parameter, id by default.
modela stringThe model the resource is about. Each action then asks the model's policy first: show asks read, store asks create, update asks update and destroy asks delete. index isn't asked, since it has no one record.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for these routes, comma separated.
Example
<KScript.Routes>
  <import path="controllers/people.ks.xml" as="people" />
  <Resource path="/people" to="people" middleware="auth" />
</KScript.Routes>
import KScript.Http
import "controllers/people.ks.xml" as people

Routes {
  Resource(path: '/people', to: people, middleware: auth)
}

#<KScript.Route>Route

A route for several HTTP methods at once.

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Routes>Routes or <Group>Group.

AttributeOptionValueDescription
path requireda stringThe path, with {name} parameters: /people/{id}. {name?} is optional and {name*} takes the rest of the path.
methods requiredget | post | put | patch | delete | options | headThe HTTP methods: [get, post].
toa function: @nameThe controller: a <fn> (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.The controller: a fn (@show) or a function of an imported script (@people.show); in XML, to="people.show". Its parameters are filled by name: path parameters, request, json, query, form, input, headers.
viewa stringAnswer with this view instead, from the views folder, with the path parameters as variables.
redirecta stringAnswer with a redirect to this address instead.
statusexpressionThe redirect's status code, 302 by default.
namea stringA name for the route, for @route('name', ...).
label_keya stringA translation key for the route's display label.
dataexpressionOptional route metadata made from a declared record type.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for this route, comma separated.
cana stringAn ability the model's policy must allow before the controller runs: can: update. It is asked about the record the controller's typed path parameter names, fn update(post: Post). A no answers 403, and a guest the ability turns away is sent to sign in (401 for an API).
wherea stringRules for the parameters: where: { id: int, slug: slug } (XML: where="id: int, slug: slug"). Rules: number, int, alpha, alnum, slug, uuid or a regular expression.
etagtrue or falsetrue tags GET answers with an ETag (a hash of the body) and answers If-None-Match with 304 Not Modified when the client already has that version.
cachea stringThe Cache-Control header for GET answers: public, max-age=300, or just a number of seconds (300 is public for 5 minutes), private, no-store...
Example
<KScript.Routes>
  <Route methods="get, post" path="/contact" to="contact.handle" />
</KScript.Routes>
import KScript.Http

Routes {
  Route(methods: [get, post], path: '/contact', to: @contact.handle)
}

#<KScript.Routes>Routes

A route file: maps HTTP methods and paths to controllers, views and redirects. kscript serve loads routes.ks.xml (or routes.kscript) and api.ks.xml (under /api, with JSON errors). Its <import>, <fn> and <let> children run on every request before the route is matched.A route file: maps HTTP methods and paths to controllers, views and redirects. kscript serve loads routes.ks.xml (or routes.kscript) and api.ks.xml (under /api, with JSON errors). Its import, fn and $name = value children run on every request before the route is matched.

Holds statements.Holds statements in { }. Scripts only.

AttributeOptionValueDescription
prefixa stringA path in front of every route in the file, such as /v1.
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_responseMiddleware for every route in the file, comma separated: built-ins (log, cors, auth, throttle:30) or <fn>s taking ($request, $next).Middleware for every route in the file, comma separated: built-ins (log, cors, auth, throttle:30) or fns taking ($request, $next).
namea stringPut in front of every route name, such as admin.
apitrue or falsetrue: errors and missing routes are answered with JSON (api.* files are API files already).
Example
<KScript.Routes>
  <import path="controllers/people.ks.xml" as="people" />
  <Middleware use="log" />
  <Get path="/" view="home.html" />
  <Get path="/people/{id}" to="people.show" name="people.show" />
</KScript.Routes>
import KScript.Http
import "controllers/people.ks.xml" as people

Routes {
  Middleware(use: log)
  Get(path: '/', view: 'home.html')
  Get(path: "/people/{id}", to: @people.show, name: 'people.show')
}

Commands

Commands are called with @, in any expression: @name(arguments).

#@abort(status: number, message?) → any

Stops the request with an error status, such as @abort(404) or @abort(403, 'Admins only').
ParameterTakes
status requirednumber
message optionalany value

Arguments can be named: @abort(…, message: …).

Example
<if $person == null><do>@abort(404)</do></if>
if $person == null {
  @abort(404)
}

#@asset_url(path: string) → string

Builds a public asset URL using assets.base and the optional assets.manifest map in project configuration.
ParameterTakes
path requiredstring
Example
<echo>{ @asset_url('css/site.css') }</echo>
echo @asset_url('css/site.css')

#@etag(value, weak?: bool) → string

Tags the answer with an ETag made from a value (such as a record's version or updated time). When the request's If-None-Match already has it, the request stops here with 304 Not Modified, so the rest of the controller doesn't run.
ParameterTakes
value requiredany value
weak optionala flag: weak or not weak

Arguments can be named: @etag(…, weak: …).

Example
<do>@etag($person.updated_at)</do>
@etag($person.updated_at)

#@redirect(to, status?: number) → http_response

A redirect to another address (302 Found by default; 301 for a permanent move).
ParameterTakes
to requiredany value
status optionalnumber

Arguments can be named: @redirect(…, status: …).

Example
<return value="@redirect('/people')" />
return @redirect('/people')

#@redirect_back(default?, status?: number) → http_response

A redirect to the page the request came from (its Referer), the usual answer to a form that didn't validate. Only a page on this site counts: a Referer from anywhere else, or none, goes to default (/ when left out).
ParameterTakes
default optionalany value
status optionalnumber

Arguments can be named: @redirect_back(…, status: …).

Example
<return value="@redirect_back('/posts')" />
return @redirect_back('/posts')

#@response(body?, status?: number, type?, headers?: map) → http_response

A response with a status code and headers. A map or list body is sent as JSON, text as HTML, and a base64 value as raw bytes. Set type to override the default Content-Type.
ParameterTakes
body optionalany value
status optionalnumber
type optionalany value
headers optionalmap

Arguments can be named: @response(…, status: …, type: …).

Example
<return value="@response($person, status: 201)" />
return @response($person, status: 201)

#@route(name: string(min: 1), params...) → string

Returns the URL for a named route. Pass the route name as nonempty text and supply the values for its path parameters. For example, @route('people.show', id: 3) returns /people/3. Extra named values become query parameters.
ParameterTakes
name requiredstring(min: 1)
paramsany value, any number of them

Arguments can be named: @route(…, params: …).

Example
<a href="{ @route('people.show', id: $p.id) }">{ $p.name }</a>
a(href: @route('people.show', id: $p.id)) "{ $p.name }"

#@routes(kind?: page|http, data_type?: type, locale?: string) → list

Lists declared HTTP routes and canonical file-based pages. Filter by kind or the declared type of attached route data.
ParameterTakes
kind optionalone of these words: page|http
data_type optionaltype. A declared record type used by the routes' data option.
locale optionalstring

Arguments can be named: @routes(…, data_type: …, locale: …).

Example
<for each="route" in="@routes(kind: 'page')">
  <echo>{ $route.path }</echo>
</for>
for route in @routes(kind: 'page') {
  echo $route.path
}

#@route_is(name_or_pattern: string) → bool

True if the current request matched this route name or canonical pattern.
ParameterTakes
name_or_pattern requiredstring
Example
<if @route_is('posts.show')>
  <echo>Reading a post</echo>
</if>
if @route_is('posts.show') {
  echo 'Reading a post'
}

#@route_match(path: string, method?: string) → map

Looks up a path and method without dispatching it. Returns status, route, parameters, and allowed methods.
ParameterTakes
path requiredstring
method optionalstring

Arguments can be named: @route_match(…, method: …).

Example
<let name="found" value="@route_match('/posts/42')" />
<echo>{ $found.status } { $found.route } { $found.parameters }</echo>
$found = @route_match('/posts/42')
echo "{ $found.status } { $found.route } { $found.parameters }"

#@signed_url(to: string(min: 1), expires?: duration, params...) → string

A link only this app could have made, for unsubscribe, verify and download links: a route's name with its parameters (as @route() takes them), or a path, with a signature added. expires: makes it stop working after that long, such as 7d. Check it with the signed middleware or $request.has_valid_signature; changing any part of the link breaks it.
ParameterTakes
to requiredstring(min: 1)
expires optionalduration
paramsany value, any number of them

Arguments can be named: @signed_url(…, expires: …, params: …).

Example
<a href="{ @signed_url('/unsubscribe', user: 42, expires: 7d) }">Unsubscribe</a>
a(href: @signed_url('/unsubscribe', user: 42, expires: 7d)) "Unsubscribe"

#@sitemap(paths: list, origin: string) → string

An XML sitemap for an explicit list of public paths, with the given HTTP or HTTPS origin.
ParameterTakes
paths requiredlist
origin requiredstring

Arguments can be named: @sitemap(…, origin: …).

Example
<let name="paths" value="['/', '/about', '/posts/hello']" />
<echo>{ @sitemap($paths, 'https://example.com') }</echo>
$paths = ['/', '/about', '/posts/hello']
echo @sitemap($paths, 'https://example.com')

#@view(name, data...) → http_response

Renders a page from the views folder (or the site folder) with its layout, giving it the named values as variables: @view('people/show.html', person: $p). The extension may be left off. status: sets the answer's status code: @view('auth/login', error: $message, status: 422).

Arguments can be named: @view(…, data: …).

Example
<return value="@view('people/show', person: $person)" />
return @view('people/show', person: $person)

Methods

On http_response values: ->header(), ->with_status(), ->with_header(), ->with_type(), ->with_cookie(), ->without_cookie(), ->with_etag(), ->with_cache()

Variables

VariableTypeDescription
$requestrequestThe HTTP request. Inside a matched route it also contains the client ip, parsed json, combined input and route name.