Skip to content

Templates

Layouts and includes for plain HTML, and s= in server templates and components. Template code is left exactly as written.

Layouts and includes

Plain HTML pages can share a header, a footer or a whole layout. Put the shared parts in a folder whose name starts with _: HTML files there are used by pages, never written to the output folder on their own.

<!-- src/_partials/footer.html -->
<footer s="pad 24 text-muted">Made with Kerf</footer>

<!-- any page -->
<kerf-include src="_partials/footer.html"></kerf-include>

A layout is a file with <slot> elements where each page's content goes. A page wraps its content in <kerf-layout>; children with slot="name" fill <slot name="name">, and the rest fills the unnamed <slot>. Whatever is inside a <slot> is used when a page doesn't fill it.

<!-- src/_layouts/base.html -->
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <slot name="head"><title>My site</title></slot>
  <link rel="stylesheet" href="/kerf.css">
</head>
<body>
  <kerf-include src="_partials/nav.html"></kerf-include>
  <main s="max 1024 pad 24"><slot></slot></main>
</body>
</html>

<!-- src/about/index.html -->
<kerf-layout src="_layouts/base.html">
  <title slot="head">About</title>
  <h1 s="3xl bold">About</h1>
</kerf-layout>
  • src is a path in the input folder, the same from any page. <kerf-include> and <kerf-layout> work the same way; the names say what you mean. Both can be self-closed with />.
  • Layouts can use other layouts and includes, and pass their slots on: <slot name="head" slot="head"></slot>.
  • Links in a layout or include that point at the page being built get aria-current="page", so a shared nav marks the current page. Write links there as paths from the site root (/about/): a relative link is read from the page, not the file it's written in.
  • Checks see the page as a whole: text in a page is checked against the backgrounds of its layout. Errors point at the file and line they were written in, and an error in a layout is reported once, however many pages use it.
  • <slot> inside <template> is left alone, for web components.

Layouts are a kerf build feature. With Vite or Next.js, use your framework's components.

Supported files

FilesFormat
.html, .htmHTML
.php, .phtmlPHP, WordPress themes
.blade.phpLaravel Blade
.twig, .njk, .jinja, .jinja2, .liquidTwig, Nunjucks, Jinja, Liquid
.erb, .ejsERB, EJS
.hbs, .handlebars, .mustacheHandlebars, Mustache
.vue, .svelte, .astroVue, Svelte, Astro
.jsx, .tsxReact and other JSX (writes className)

How it works

Kerf reads each template as HTML with the template code set aside, rewrites s=, and puts everything else back byte for byte. Line numbers in errors are the template's own.

<ul s="stack 8">
<?php foreach ($items as $item): ?>
  <li s="pad 8"><?= esc_html($item) ?></li>
<?php endforeach; ?>
</ul>

Contrast is followed through each file's own nesting. A server-template partial (a PHP include, a Blade or Twig partial) is checked as if it sits on the page background, since one file can't know where it will be included. <kerf-include> partials are different: they're checked inside each page that uses them.

Classes chosen at runtime

s= is read when you build, so it must be fixed words. For styling that changes at runtime, write k- classes in your code: Kerf finds them, checks them and includes them in the stylesheet.

<?php $state = $open ? 'k-show' : 'k-hide'; ?>
<nav class="<?= $state ?>" s="row 16">

<div :class="{ 'k-hide': !open }" s="pad 16">           <!-- Vue -->

<nav className={open ? "k-show" : "k-hide"} s="row 16"> {/* JSX */}

A class attribute with code in it is extended, not replaced: class="<?= $c ?>" becomes class="<?= $c ?> k-row-16", and in JSX className={expr} is combined with Kerf's classes.

Building a theme

Server templates are compiled by kerf build, which writes them to outDir with everything else copied. Point input at your source and outDir at the folder your server reads:

// a WordPress theme
input: "theme-src",
outDir: "wp-content/themes/mysite",

// Laravel views
input: "resources/kerf",
outDir: "resources/views",

Use kerf build --watch while you work. JSX, Vue, Svelte and Astro go through the Vite plugin or, in Next.js, withKerf instead.

Limits

  • s= inside a JSX attribute expression (icon={<Icon s="pad 4" />}) isn't compiled; in children, including .map(), it is.
  • Uppercase components like <Input /> aren't checked as HTML elements.