KScript docs
EN

Web pages

Template props and component scripts

A layout or component may declare the inputs it takes with <prop>, at its top level:

HTML page
<!-- _layout.html -->
<prop name="page-title" required />
<prop name="subtitle" />
<h1>{$page_title}</h1>
<slot />

Call the layout with <layout path="_layout.html" page-title="Home">…</layout>. The editor suggests the declared props, and reports a missing required value for a known layout path. Rendering checks required props even for a layout path worked out while the page runs. Markdown pages pass layout props in front matter. An optional prop is null when it is left out.

Component scripts

Put note.kscript next to _components/note.html, and the component's template can call the script's functions. An optional load(props: map) function receives the props and gives the template $data. The template may also read $props, and each prop as its own variable:

<fn name="load" params="props: map" value="{ title: $props.title->trim() }" />
<fn name="heading" params="title: string" value="$title->upper()" />
fn load(props: map) => { title: $props.title->trim() }
fn heading(title: string) => $title->upper()
HTML page
<!-- _components/note.html -->
<prop name="title" required />
<article>{@heading($data.title)}<slot /></article>

The page using the component may pass onLoad, a callback that runs on the server with the result of load (or null when the component has no load):

HTML page
<Note title="A note" onLoad={fn(data) => @note_loaded($data)}>
  <p>Content supplied by the page</p>
</Note>

Loading may run again when the component renders, including during live redraws, so keep render-time callbacks free of lasting writes.

Edit this page on GitHub