KScript docs
EN

KScript.Web

Web pages run by kscript serve: layouts, components and live blocks.

Import it at the top of a script:

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

It brings KScript.Markdown with it.

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

ElementsStatements

#<include>include

Renders another page file here. Other attributes become its variables.

Self-closing.A statement on its own. Pages only. Other attributes become variables.Other options become variables.

AttributeOptionValueDescription
path requireda file pathThe file to include, relative to this page.
Example
<include path="_partials/card.html" title="Welcome" body="Thanks for signing up." />
include(path: '_partials/card.html', title: 'Welcome', body: 'Thanks for signing up.')

#<layout>layout

Wraps the page in a layout file. Extra attributes become variables in the layout ($title).

Holds HTML and KScript elements. Pages only. Other attributes become variables.Other options become variables.

AttributeOptionValueDescription
path requireda file pathThe layout file, such as _layout.html.
Example
<layout path="_layouts/main.html" title="About us">
  <h1>About us</h1>
  <p>We make tools for people who write scripts.</p>
</layout>
layout(path: '_layouts/main.html', title: 'About us') {
  h1 "About us"
  p "We make tools for people who write scripts."
}

#<live>live

A reactive block: its on-* handlers run on the server and only this block is redrawn. No page JavaScript needed.

Holds HTML and KScript elements. Pages only.

AttributeOptionValueDescription
ida stringA name for the block, unique on the page. Omit it to assign one in render order.
lazytrue or falseLoad the block when it enters the viewport.
defertrue or falseLoad the block after the initial document is ready.
Example
<live id="counter">
  <state name="count" value="0" />
  <button on-click="$count += 1">{$count}</button>
</live>
live(id: 'counter') {
  state(name: count, value: 0)
  button(on-click: { $count += 1 }) "{$count}"
}

#<markdown>markdown

Renders its Markdown text (with { } values, escaped) to HTML.

Holds text with { } values. Pages only.

Example
<markdown>
# Hello, {$name}

You have **{$count}** new messages.
</markdown>
markdown "# Hello, {$name}

You have **{$count}** new messages."

#<placeholder>placeholder

What a lazy or deferred live component shows before it loads.

Holds HTML and KScript elements. Pages only. Goes inside <live>live.

Example
XML
<live id="report" lazy>
  <placeholder><p>Loading the report…</p></placeholder>
  <state name="day" value="@today()" />
  <p>The report for {$day}</p>
</live>

#<prop>prop

Declares a value a layout or component receives from its caller.

Self-closing.A statement on its own. Pages only.

AttributeOptionValueDescription
name requireda stringThe input name, available as a variable in the template.
requiredtrue or falseRequire the caller to supply a non-null value.
Example
XML
<prop name="title" required />

#<respond>respond

Ends the request with this response. header-<name>="..." attributes send headers, such as header-set-cookie.

Holds text with { } values. Pages only.

AttributeOptionValueDescription
statusexpressionThe HTTP status code.
jsonexpressionSend this value as JSON.
redirecta string with { }Send the visitor to this address instead. The status is 302 unless status says otherwise.
typea stringThe Content-Type.
Example
<respond status="404">Not found</respond>
<respond json="$people" />
<respond redirect="/thanks" />
respond(status: 404) "Not found"
respond(json: $people)
respond(redirect: '/thanks')

#<state>state

A variable of a <live> block that keeps its value between events.A variable of a live block that keeps its value between events.

Self-closing.A statement on its own. Pages only. Goes inside <live>live.

AttributeOptionValueDescription
name requireda new variable's nameThe variable to declare.
value requiredexpressionIts starting value.
sessiona stringA session key used to keep this state across page visits.
lockedtrue or falseOnly actions on the server can change this state.
querya stringA URL query parameter synchronized with this state.
Example
XML
<live id="search"><state name="query" value="''" /><input @bind($query)></live>

Commands

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

#@emit_event(name, data?, target?)

Sends an event from a live action to the page's live blocks. A target limits it to one block ID.

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

Example
<echo>{ @emit_event('items-changed', data: $item.id, target: 'summary') }</echo>
echo @emit_event('items-changed', data: $item.id, target: 'summary')

#@live_download(name, bytes: base64)

Downloads bytes from a live action as a file. Use a filename without directory separators.
ParameterTakes
name requiredany value
bytes requiredbase64

Arguments can be named: @live_download(…, bytes: …).

Example
<echo>{ @live_download('report.txt', bytes: 'Hello'->to_base64()) }</echo>
echo @live_download('report.txt', bytes: 'Hello'->to_base64())

#@live_redirect(path)

Sends the browser to a path on this site after a live action.
Example
<echo>{ @live_redirect('/account') }</echo>
echo @live_redirect('/account')

#@skip_render()

Keeps the current HTML after a live action while saving its state and sending any effects.
Example
<echo>{ @skip_render() }</echo>
echo @skip_render()

#@stream(target, text, mode?: append|replace, html?: bool)

Sends an update to a visible live-stream target before the action finishes. Text is literal by default. Use mode: replace to replace the target's contents, or html: true for trusted HTML.
ParameterTakes
target requiredany value
text requiredany value
mode optionalone of these words: append|replace
html optionala flag: html or not html

Arguments can be named: @stream(…, text: …, mode: …).

Example
<echo>{ @stream('progress', 'Working…', mode: replace) }</echo>
echo @stream('progress', 'Working…', mode: replace)

#@visit(path: string, options?: map, bearer?: string) → page_visit

For tests: renders a page of this project as a browser gets it, the way kscript serve answers it: its routes first (a view: route or a controller, with their middleware), then the file pages under serve.root. options takes headers, as @api() does ({ headers: { Cookie: 'guest=abc' } }), and @acting_as() signs the visit in. ->click('Add') runs a live handler, ->fill('draft', 'milk') types into a bound input, and .text, .html and ->classes('Buy milk') show the result.
ParameterTakes
path requiredstring
options optionalmap
bearer optionalstring

Arguments can be named: @visit(…, options: …, bearer: …).

Example
<let name="page" value="@visit('/tasks')->fill('draft', 'Buy milk')->click('Add')" />
<echo>{ $page.text }</echo>
$page = @visit('/tasks')->fill('draft', 'Buy milk')->click('Add')
echo $page.text

Methods

On page_visit values: ->click(), ->change(), ->press(), ->submit(), ->fill(), ->classes()