KScript.Web Web pages run by kscript serve: layouts, components and live blocks.
Import it at the top of a script:
.kscript XML Copy
<import package="KScript.Web" />.kscript XML Copy
import KScript.WebIt 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.
Elements Statements # <include>includeRenders 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.
Attribute Option Value Description path required a file path The file to include, relative to this page.
Example
.kscript XML Copy
<include path="_partials/card.html" title="Welcome" body="Thanks for signing up." />.kscript XML Copy
include(path: '_partials/card.html', title: 'Welcome', body: 'Thanks for signing up.')# <layout>layoutWraps 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.
Attribute Option Value Description path required a file path The layout file, such as _layout.html.
Example
.kscript XML Copy
<layout path="_layouts/main.html" title="About us">
<h1>About us</h1>
<p>We make tools for people who write scripts.</p>
</layout>.kscript XML Copy
layout(path: '_layouts/main.html', title: 'About us') {
h1 "About us"
p "We make tools for people who write scripts."
}# <live>liveA 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.
Attribute Option Value Description ida string A 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
.kscript XML Copy
<live id="counter">
<state name="count" value="0" />
<button on-click="$count += 1">{$count}</button>
</live>.kscript XML Copy
live(id: 'counter') {
state(name: count, value: 0)
button(on-click: { $count += 1 }) "{$count}"
}# <markdown>markdownRenders its Markdown text (with { } values, escaped) to HTML.
Holds text with { } values. Pages only.
Example
.kscript XML Copy
<markdown>
# Hello, {$name}
You have **{$count}** new messages.
</markdown>.kscript XML Copy
markdown "# Hello, {$name}
You have **{$count}** new messages."# <placeholder>placeholderWhat a lazy or deferred live component shows before it loads.
Holds HTML and KScript elements. Pages only. Goes inside <live>live .
Example
XML Copy
<live id="report" lazy>
<placeholder><p>Loading the report…</p></placeholder>
<state name="day" value="@today()" />
<p>The report for {$day}</p>
</live># <prop>propDeclares a value a layout or component receives from its caller.
Self-closing. A statement on its own. Pages only.
Attribute Option Value Description name required a string The input name, available as a variable in the template. requiredtrue or falseRequire the caller to supply a non-null value.
Example
XML Copy
<prop name="title" required /># <respond>respondEnds the request with this response. header-<name>="..." attributes send headers, such as header-set-cookie.
Holds text with { } values. Pages only.
Attribute Option Value Description statusexpression The HTTP status code. jsonexpression Send this value as JSON. redirecta string with { } Send the visitor to this address instead. The status is 302 unless status says otherwise. typea string The Content-Type.
Example
.kscript XML Copy
<respond status="404">Not found</respond>
<respond json="$people" />
<respond redirect="/thanks" />.kscript XML Copy
respond(status: 404) "Not found"
respond(json: $people)
respond(redirect: '/thanks')# <state>stateA 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 .
Attribute Option Value Description name required a new variable's name The variable to declare. value required expression Its starting value. sessiona string A session key used to keep this state across page visits. lockedtrue or falseOnly actions on the server can change this state. querya string A URL query parameter synchronized with this state.
Example
XML Copy
<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
.kscript XML Copy
<echo>{ @emit_event('items-changed', data: $item.id, target: 'summary') }</echo>.kscript XML Copy
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.
Parameter Takes name required any value bytes required base64
Arguments can be named: @live_download(…, bytes: …).
Example
.kscript XML Copy
<echo>{ @live_download('report.txt', bytes: 'Hello'->to_base64()) }</echo>.kscript XML Copy
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
.kscript XML Copy
<echo>{ @live_redirect('/account') }</echo>.kscript XML Copy
echo @live_redirect('/account')# @skip_render ()Keeps the current HTML after a live action while saving its state and sending any effects.
Example
.kscript XML Copy
<echo>{ @skip_render() }</echo>.kscript XML Copy
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.
Parameter Takes target required any value text required any value mode optional one of these words: append|replace html optional a flag: html or not html
Arguments can be named: @stream(…, text: …, mode: …).
Example
.kscript XML Copy
<echo>{ @stream('progress', 'Working…', mode: replace) }</echo>.kscript XML Copy
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.
Arguments can be named: @visit(…, options: …, bearer: …).
Example
.kscript XML Copy
<let name="page" value="@visit('/tasks')->fill('draft', 'Buy milk')->click('Add')" />
<echo>{ $page.text }</echo>.kscript XML Copy
$page = @visit('/tasks')->fill('draft', 'Buy milk')->click('Add')
echo $page.textMethods On page_visit values: ->click() , ->change() , ->press() , ->submit() , ->fill() , ->classes()