KScript docs
EN

Web pages

Introduction

kscript serve runs a folder as a website. Every .html and .md page in it is a template: KScript elements sit directly in the HTML, { expression } works in text and attributes, and the visitor gets plain HTML. There is no build step, and no JavaScript to write.

To get started, serve a folder and open it in a browser:

CommandWhat it does
kscript serve "Example Sites/team" --port 8080Then open http://localhost:8080.

Here is a page that reads a JSON file and lists the people in it:

HTML page
<!-- people.html -->
<let name="people" value="@read_json(./data/people.json)" />
<let name="min" value="($request.query.min_age ?? '0')->to_number(default: 0)" />

<layout path="_layout.html" title="People">
  <h1>People aged {$min} and over</h1>
  <ul>
    <for each="p" in="$people |> filter(fn(p) => $p.age ge $min) |> sort(by: .name)">
      <li class="{ $p.age ge 65 ? 'senior' : '' }">
        <a href="/people/{$p.id}">{$p.name}</a> ({$p.age})
      </li>
    </for>
    <else>
      <li>Nobody matches.</li>
    </else>
  </ul>
</layout>

See Example Sites/team for a complete site with a layout, a component, a Markdown page, a dynamic route and a JSON endpoint. For controllers, JSON APIs and middleware, add a route file: see Routes, controllers and middleware. Routes are matched before pages, and anything they don't match is served as described here.

Live reload

During development, open the site in a browser and save a page, component, script, stylesheet or asset. Open HTML pages reload by themselves. kscript serve --no-reload turns that off. Production and compiled sites serve pages without the reload script.

Each request runs your scripts fresh, so a save shows up on the next request without restarting the server. The server doesn't parse every script again for that, though: it parses each script once and keeps the result, and parses a script again only when its text changes. When you save one controller, that controller is the only file parsed again. The pages and route files that import it pick up the new version on the next request. The route table is built again when a route file changes, or any script or file the routes reach does, such as a controller or a config file.

For a development site in a container, add a Compose file and run kscript serve, which then serves from Docker. --no-docker serves on the host instead, and serve.docker in kscript.json makes Docker the default. See Running the development site in Docker.