First priority: route and page discovery
kscript routes -v prints declared HTTP routes and their source locations. @route(name, ...) builds a path from a known route name. The OpenAPI export describes the API routes selected by its configuration. Before @routes(), scripts had to gather those sources separately. This makes it harder to build a sitemap, generate navigation, check links, or test the shape of a project's routing table. See the HTTP guide, web guide, and the route listing implementation.
Route records from @routes()
Return structured route records for the current project. A page can filter these records for navigation, and a test can assert that a path is registered.
<let name="pages" value="@routes(kind: page)" />
<let name="post" value="$pages->find(fn(entry) => $entry.path == '/posts/{slug}')" />
<echo value="$post.parameters" /> <!-- ['slug'] -->$pages = @routes(kind: page)
$post = $pages->find(fn(entry) => $entry.path == '/posts/{slug}')
echo $post.parameters // ['slug']| Field | Meaning |
|---|---|
kind |
'page' for file-based pages or 'http' for an explicit declaration. |
path |
Canonical URL pattern. For example, index.html produces /, and posts/[slug].md produces /posts/{slug}. Dynamic patterns have no concrete URL until their parameters are supplied. |
methods |
HTTP methods explicitly accepted by the entry, including implied HEAD for a declared GET. Any-method routes and file-based pages have an empty methods list and any_method: true. |
name |
Configured route name, or null. |
parameters |
Path parameter names in path order. |
label_key, label |
Optional translation key and its resolved display text. See Localized route labels. |
data |
Optional record made from a declared type. See Typed route data. |
- Default to both kinds. Support
kind: pageandkind: httpso scripts can ask for one. Sites with no explicit route files still return their file-based pages. - Use the same page exclusions and precedence as the live resolver. Keep one canonical entry for each page. Respect reserved directories, configured views, static assets, explicit route ownership, and HTML versus Markdown precedence. Represent dynamic patterns without inventing parameter values. The listing uses canonical paths without locale prefixes. A dynamic pattern remains in the listing when an explicit route shadows only some of its URLs.
- Read route metadata from the current app's cached or in-flight route table. Listing from a request must not render a page, dispatch a controller, call middleware, or recursively load the route files. Define an outside-request path for project scripts and tests that follows the existing route-loading rules.
- Keep introspection separate from authorization. A route listing can include protected patterns, so public navigation or sitemap code must choose what to expose. Leave handler internals and filesystem locations to development tooling.
kscript routes --output jsonprints the same record layout and keeps app-specific data out of the export. The terminal table remains the default.- Test file-only sites, dynamic folders, index pages, duplicate aliases, hidden templates, explicit routes that own a page, grouped and resource routes, and method handling. Add a rendered HTML example that uses
<for>to consume the result.
Implementation starting points are RouteTable, the cached HttpApp table, and PageSite.Resolve.
Localized route labels
A named route has a stable identifier such as home, used by @route('home'). The route declaration has no field for a reader-facing label such as Home or Maison. Today a template can call @t('routes.home'), but it has to know the translation key itself. Markdown front matter can supply a page title, which is separate from route metadata.
Add an optional label_key to the route declaration. Resolve it through the existing translation catalog when listing or presenting routes.
<Get path="/" view="home.html" name="home" label_key="routes.home" />Get(path: '/', view: 'home.html', name: 'home', label_key: 'routes.home')| Translation file | Entry |
|---|---|
locales/en.json |
{ "routes": { "home": "Home" } } |
locales/fr.json |
{ "routes": { "home": "Maison" } } |
@routes()resolves labels using the current locale. The optionallocale:argument lets a script build navigation for another locale.- Use the configured fallback locale for a missing translation. Return
nullfor the display label when the route has no key or no translation exists in the fallback chain. Keep the key available for diagnosis. - Keep the route name and path stable across languages. Build localized links with the existing
@locale_path(). A Markdown page can setlabel_keyin its front matter. In HTML, use<meta name="kscript:label-key" content="routes.about">near the top of the page. - Verify the same route produces Home and Maison from the two catalogs. Cover locale fallback, unnamed routes with a label, and HTML-escaped labels in a rendered navigation list.
See Translations in the language guide and the existing HTTP route attributes.
Typed route data
A route can optionally carry app-specific data for navigation, breadcrumbs, or sitemaps. Give that data a declared record type so required fields and their types can be checked where the route is written.
Define the type once in types/navigation-data.kscript.
<type name="NavigationData">
<field name="order" type="int" />
<field name="icon" type="string" />
</type>type NavigationData {
order: int
icon: string
}Import it into the route file and pass a record constructed from it to the data: argument. The constructor gives the editor the exact fields and types at the call site.
<KScript.Routes>
<import path="../types/navigation-data.kscript" />
<Get path="/home" name="home" view="home.html" label_key="routes.home" data="NavigationData(order: 10, icon: 'home')" />
</KScript.Routes>import KScript.Http
import "../types/navigation-data.kscript"
Routes {
Get(
path: '/home',
name: 'home',
view: 'home.html',
label_key: 'routes.home',
data: NavigationData(order: 10, icon: 'home')
)
}A page imports the same declaration. The data_type: filter selects routes carrying that type and gives the loop a typed $entry.data. The template can sort links by the declared order and use the translated label.
<import package="KScript.Http" />
<import path="types/navigation-data.kscript" />
<nav aria-label="Main navigation">
<ul>
<for each="entry" in="@routes(data_type: NavigationData)->sort(by: .data.order)">
<li>
<a href="{ @route($entry.name) }"
data-icon="{ $entry.data.icon }">{ $entry.label }</a>
</li>
</for>
</ul>
</nav>- The route can omit
data. When supplied, it must be a value of a declared record type. The constructor applies that type's existing validation rules. Routes with no attached data havedata: null. - Store the type identity with the record. A typed listing filters by declaration, so a record with the same fields from another type does not accidentally appear in the menu. Completion should offer the declared fields on
$entry.data, including in HTML. - The illustrated menu uses named routes without path parameters. A dynamic route needs concrete parameter values before it can become a link. Attaching data has no effect on authorization.
- Keep app-specific route data out of public exports by default. Test a missing type, a missing required field, two different record types with the same fields, and an empty typed listing.
- The equivalent XML argument is
data="NavigationData(order: 10, icon: 'home')". Both forms use the same constructor. KScript already uses square brackets for indexing, such as$data[key].
Type arguments and constraints
Generalize the existing type-as-value pattern from calls such as ->convert(from: json, as: Repo). A method should be able to require a type argument, constrain which types it accepts, and use that argument to check a companion value or describe its result. Requiring a declared record for route data is one use of that design.
| Proposed constraint | What it would guarantee |
|---|---|
Record |
The caller supplies a declared record type. The editor can suggest its fields, catch misspelled keys, and report missing required fields. Runtime checks also cover values built dynamically. |
Array |
The caller supplies a KScript list type, optionally with a known element type. Use this when an API specifically needs list ordering, indexing, or list methods. |
Iterable |
The value can be traversed. KScript's for already accepts several kinds, including lists, maps, ranges, and text. Specify what an iteration yields for every kind the constraint admits. |
Mappable |
The value has a mapping contract. Define callback arguments and result shape before admitting multiple kinds. Start with Array when the method needs list mapping specifically. |
- Resolve a known type at the call site. The language server should complain at the type argument if the constraint fails, and at the offending field or value if the data fails its type check.
- Validate the type and constraint again at runtime when the caller gets a value dynamically. A valid type argument supplies a schema even when the analyzer cannot see the data.
- Keep type-level constraints reusable across built-in commands and methods. Decide the declaration syntax for user-defined functions separately after checking how type arguments flow into callbacks and return values.