Built-in variables
Variables every script or page has without declaring them. Some come with a package, and exist once it is imported.
| Variable | Type | Package | Description |
|---|---|---|---|
$args | list | KScript.Core | The command line arguments after the script path. |
$config | config | KScript.Core | The project's settings: the "config" section of kscript.json, with kscript.<env>.json and config.kscript laid over it: $config.database.host. |
$env | map | KScript.Core | Environment variables: $env.HOME, $env['PATH']. |
$event | map | KScript.Core | The event a handler runs for, in on-* attributes and <on> blocks: $event.value, $event.key, $event.data... Its fields depend on the handler. |
$log | logger | KScript.Core | The script's logger: $log->info('Saved', { id: $id }), $log->named('db')->debug(...). Providers and levels come from $config.logging. |
$os | map | KScript.Core | The computer: $os.name (windows, macos or linux), $os.user, $os.machine, $os.home, $os.temp, $os.separator, $os.newline. |
$script | map | KScript.Core | This script: $script.path, $script.dir, $script.name. |
$session | session | KScript.Auth | The request's session: values kept between requests by a cookie. $session->set('cart', $items), $session->get('cart'), and $session->flash('status', 'Saved') for a message after a redirect. |
$cache | cache | KScript.Cache | The app's cache in memory: $cache->remember('repos', fn() => @get_json($url), ttl: 10m). It lasts while kscript runs, so in kscript serve it is shared by every request. @cache(file) keeps values between runs. |
$db | database | KScript.Data | The default database connection: $db->table('users'), $db->tables(), $db->create_table(User). With no config it is SQLite in data/app.sqlite in the project folder; database.connections in your config sets others. |
$request | request | KScript.Http | The HTTP request. Inside a matched route it also contains the client ip, parsed json, combined input and route name. |
$e | number | KScript.Math | e, about 2.71828. |
$pi | number | KScript.Math | π, about 3.14159. |
$tau | number | KScript.Math | τ (2π), about 6.28318. |
$vars | map | KScript.Style | Values given to the stylesheet: @stylesheet(path, vars) or kscript css --var name=value. |