KScript docs
EN

Types and validation

Slash paths

A slash_path is a list of names joined by /: where a page sits on a site, a menu or category path, a route segment or a key in a tree. It reads like a URL path, but it never touches the disk. A file or folder is a file_path, so the type has its own name.

<let name="menu" type="slash_path" value="/My Applications/Something" />
<let name="site" type="slash_path" value="/my-website" />
<echo>{ $menu.name } { $menu.parent } { $menu.depth } { $menu.segments }</echo>
<echo>{ $site.parent.is_root } { $site->join('blog/../about') }</echo>

<let name="docs" value="/docs/guides/start" />
<echo>{ $docs->is_within(/docs) } { $docs.parent.name }</echo>

<fn name="open" params="page: slash_path" value="&quot;Opening { $page }&quot;" />
<echo value="@open(/help/contact)" />
$menu: slash_path = /My Applications/Something
$site: slash_path = /my-website
echo "{ $menu.name } { $menu.parent } { $menu.depth } { $menu.segments }"
echo "{ $site.parent.is_root } { $site->join('blog/../about') }"

$docs = /docs/guides/start
echo "{ $docs->is_within(/docs) } { $docs.parent.name }"

fn open(page: slash_path) => "Opening { $page }"
echo @open(/help/contact)

A bare slash path supports interpolation: $name and { expression } are worked out, and a name in braces, such as {id}, stays as written because it is a route parameter. A value with / in it adds more names. Text becomes a slash path wherever one is expected:

<let name="id" value="42" />
<let name="section" value="'Guides'" />
<echo value="/people/$id/edit" />
<echo value="/docs/{ $section->lower() }/start" />
<echo value="/people/{id}" />

<let name="note" value="{ path: 'home' }" />
<let name="where" type="slash_path" value="$note.path" />
<echo value="$where" />
$id = 42
$section = 'Guides'
echo /people/$id/edit
echo /docs/{ $section->lower() }/start
echo /people/{id}

$note = { path: 'home' }
$where: slash_path = $note.path
echo $where

A route's path: is a slash path too, so Get(path: /people/{id}) and Get(path: "/people/{id}") are the same route. A route never fills in values: {id} there is matched against the URL. The project templates write route paths without quotes.

The value is tidied when it is made: ' / My Apps // Notes / ' becomes /My Apps/Notes, /docs/./guides/../start becomes /docs/start, and /blog/ becomes /blog. A slash path always starts with /, so text without one gets it: 'home' and 'home/' are both /home, and / on its own (or '') is the top. Case is kept, so /Docs and /docs are different paths. .. can't go above /, and \ isn't a separator, so 'C:\Apps' fails with the reason.

Field or method What it gives
.segments The names in order: ['My Applications', 'Something']. / has none.
.name The last name: Something, or '' for /.
.parent The path one up, as a slash_path: /My Applications, then /. It is null for /.
.depth How many names it has: 2.
.is_root True for / itself.
->join(more) More names on the end, tidied: /docs->join('guides/../start') is /docs/start. A / at the start of more is ignored.
->is_within(other) True when it is the other path or under it. Names are compared exactly.

The editor checks slash paths written in code (KS5012), suggests their fields and methods, and colours /docs/start as a value.