KScript docs
EN

Web pages

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:

HTML page
<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.

Variables

<let> declares a variable in the current block, and <set> changes one. <do> runs a few statements:

HTML page
<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:

HTML page
<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:

HTML page
<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:

HTML page
<!-- _layout.html -->
<html>
  <head><title>{$title}</title></head>
  <body><slot /></body>
</html>
HTML page
<!-- 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:

HTML page
<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:

md
---
title: About
layout: _layout.html
---
# About {$title}

This page was written in Markdown.

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:

HTML page
<if test="$user == null">
  <respond redirect="/sign-in" />
</if>
<respond status="404" />

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.

Edit this page on GitHub