Templates
Displaying values
{ value } puts a value into the page, HTML-escaped (Markdown-escaped in .md pages). {! value } puts in trusted HTML as it is, and {{ and }} are literal braces:
<p>Hello, {$user.name}</p>
<div>{! $post.html }</div>
<code>{{ not a value }}</code>An attribute that is exactly { value } is left out when the value is false or null, so checked="{$done}" just works.
Expressions are KScript's: + - * / %, == and != (loose, so '3' == 3), === and !== (strict: type and value), ??, ?., a ? b : c, in, lists, maps, $p.name, $list[0], ->method(), |> and fn(x) => …. In attributes, use the words lt, le, gt, ge, and, or and not, since XML can't hold a raw < or && there.
Comments
An HTML comment, <!-- like this -->, is sent to the browser as it is. A comment that opens with three dashes, <!--- like this --->, is a note for whoever edits the template, and never reaches the page:
<!--- The card for one product: its logo, name and line. --->
<article class="card">…</article>Variables
<let> declares a variable in the current block, and <set> changes one. <do> runs a few statements:
<let name="total" value="0" />
<let>$count = 3</let>
<set name="total" value="$total + 10" />
<set>$count += 1</set>
<do>$count += 1; $total = $total + 5</do>Conditionals
<if>, <elif> and <else> pick a branch. <match> picks one <case>, with <default> for anything else:
<if test="$cart->count() == 0">
<p>Your cart is empty.</p>
</if>
<elif test="$cart->count() == 1">
<p>One item.</p>
</elif>
<else>
<p>{$cart->count()} items.</p>
</else>
<match value="$order.status">
<case is="'paid', 'shipped'">Thanks for your order.</case>
<case when="$it == 'refunded'">Refunded.</case>
<default>Waiting for payment.</default>
</match>Loops
<for> loops over a list, or over a map's keys and values. at names the position, and $loop.index and $loop.first are set too. An <else> straight after a <for> runs when there was nothing to loop over:
<ol>
<for each="task" in="$tasks" at="i">
<li>{$i + 1}. {$task.title}</li>
</for>
<else>
<li>No tasks.</li>
</else>
</ol>
<for each="key, value" in="$settings">
<p>{$key}: {$value}</p>
</for><while test> loops while its test is true, and stops after 100,000 runs.
Layouts
<layout> wraps the page in a layout. The layout puts the page where its <slot /> is, and the layout tag's other attributes become variables in it:
<!-- _layout.html -->
<html>
<head><title>{$title}</title></head>
<body><slot /></body>
</html><!-- about.html -->
<layout path="_layout.html" title="About us">
<h1>About us</h1>
</layout>Components and includes
A tag such as <my-card> renders the component in _components/my-card.kscript, with <slot /> for its children. The tag matches the component's file name. <include> puts another template in place:
<my-card user="{$u}">Joined this year</my-card>
<include path="_footer.html" />Components are in the components guide.
Markdown
<markdown> renders Markdown, with { } values escaped. A .md page may start with --- front matter, such as title: About and layout: _layout.html, and its keys become variables. Inline code and fenced code blocks keep their contents as written:
---
title: About
layout: _layout.html
---
# About {$title}
This page was written in Markdown.Markdown is read the way GitHub reads it. Headings get GitHub's ids, so ## 1. About us is #1-about-us and ## Änderungen is #änderungen. A bare address such as hello@example.com becomes a mailto: link, as a bare https:// address becomes a link.
Highlighting code
->highlight() turns code into HTML for a <pre>, coloured the way the editor colours it. It reads the code with the same TextMate grammars as the VS Code extension, the engine Shiki is built on, so a page shows exactly what you see in VS Code. You don't need Node or any page JavaScript: the HTML comes from the server.
<import package="KScript.IO" />
<let name="code" value="@read_text('_examples/hello.kscript')" /> <!-- echo "Hello, { $name }!" -->
<let name="html" value="$code->highlight()" /> <!-- <span class="hl-fn">echo</span> <span class="hl-str">"Hello, </span>... -->import KScript.IO
$code = @read_text('_examples/hello.kscript') // echo "Hello, { $name }!"
$html = $code->highlight() // <span class="hl-fn">echo</span> <span class="hl-str">"Hello, </span>...Write the result with {! }, because it is HTML already: <pre><code>{! $html }</code></pre>.
lang: says which language the code is in. It's kscript (the .kscript format) unless you say otherwise. You can also use ks.xml, html for a page (its { values } and @ directives are coloured too), ks.css, or text to only escape it. Every language VS Code comes with works too: css, js, ts, json, sql, sh, yaml, python and so on. Any other name is an error that lists these.
<let name="page" value="'<p class="note">{ $name }</p>'->highlight(lang: 'html')" /> <!-- the tag, the attribute and $name each in a span -->
<let name="data" value="'{"count": 2}'->highlight(lang: 'json')" /> <!-- {"count": <span class="hl-num">2</span>} and so on -->$page = '<p class="note">{ $name }</p>'->highlight(lang: 'html') // the tag, the attribute and $name each in a span
$data = '{"count": 2}'->highlight(lang: 'json') // {"count": <span class="hl-num">2</span>} and so onEach token is a <span> with a class, and your stylesheet picks the colours:
| Class | What it marks |
|---|---|
hl-com |
a comment |
hl-str |
text in quotes |
hl-re |
a regex |
hl-num |
a number, true, false or null |
hl-kw |
a keyword: fn, if, return, import |
hl-op |
an operator: =, +, => |
hl-fn |
a function or command: @read_json, ->map, echo |
hl-type |
a type: string, int, Note |
hl-var |
a variable or a property: $name, a JSON key |
hl-tag |
an HTML or XML tag |
hl-attr |
an attribute name |
hl-expr |
the braces around a { value } in text or a page |
pre .hl-kw { color: #c6a6ff; }
pre .hl-str { color: #4febc8; }
pre .hl-com { color: #7f93bc; font-style: italic; }Sending a different response
<respond> sends something other than the page: JSON, another status such as a 404, a redirect, another content type or extra headers:
<if test="$user == null">
<respond redirect="/sign-in" />
</if>
<respond status="404" />An attribute named header- and a header's name sends that header, such as header-set-cookie="theme=dark; Path=/" or header-cache-control="no-store".
A status is from 100 to 999, and header values are plain ASCII without line breaks. Anything else is an error page that says which value was wrong, so a visitor's text put in a header can't add a header of its own. A redirect to an address with other characters, such as /café, is sent percent-encoded.