Template Primitives
The blocks a .wisp template is made of, each in a few lines: conditions, loops, match, constants, snippets, components and a streamed await.
src/components/Card.wisp
{@props title, tone: &str = "plain", count: usize = 0}
<section class=${format!("card {tone}")}>
<h3>${title}{#if count > 0} <small>(${count})</small>{/if}</h3>
{@render children()}
</section>
<style>
.card {
margin: 0 0 0.75rem;
padding: 0.75rem 1rem;
border: 1px solid var(--line);
border-radius: var(--radius);
background: var(--panel);
}
.card.accent {
border-color: var(--accent);
}
h3 {
margin: 0 0 0.25rem;
font-size: 1rem;
}
</style>
src/routes/+page.wisp
---
enum Size {
Empty,
Few,
Many,
}
// Ready in a moment: the page does not wait for it.
async fn weather() -> String {
wisp::sleep(Duration::from_millis(900)).await;
"Sunny, 21°C".to_string()
}
let size = match cx.query("size").as_deref() {
Some("empty") => Size::Empty,
Some("many") => Size::Many,
_ => Size::Few,
};
let fruit: Vec<&str> = match size {
Size::Empty => vec![],
Size::Few => vec!["Apple", "Pear"],
Size::Many => vec!["Apple", "Pear", "Plum", "Fig", "Kiwi"],
};
---
<title>Template Primitives</title>
<h1>Template Primitives</h1>
<nav class="sizes">
{#each [("empty", "Empty"), ("few", "Few"), ("many", "Many")] as (key, label)}
<a href=${format!("?size={key}")} class:on=${matches!((key, &size), ("empty", Size::Empty) | ("few", Size::Few) | ("many", Size::Many))}>${label}</a>
{/each}
</nav>
{#snippet row(name: &str, i: usize)}
<tr><td>${i + 1}</td><td>${name}</td><td>${name.len()} letters</td></tr>
{/snippet}
<Card title="The Fruit" tone="accent" count=${fruit.len()}>
{#match size}
{:case Size::Empty}<p>Nothing here.</p>
{:case Size::Few}<p>A short list.</p>
{:case Size::Many}<p>A long list.</p>
{/match}
<table>
{#each fruit.iter().enumerate() as (i, name)}
{@render row(name, i)}
{:else}
<tr><td>The bowl is empty.</td></tr>
{/each}
</table>
</Card>
<Card title="The Weather">
{#await weather()}
<p class="muted">Looking outside...</p>
{:then w}
<p>${w}</p>
{/await}
</Card>
<Card title="A Constant">
{@const total = fruit.iter().map(|f| f.len()).sum::<usize>()}
{#if total > 12}
<p>${total} letters in all: a mouthful.</p>
{:else if total > 0}
<p>${total} letters in all.</p>
{:else}
<p>No letters at all.</p>
{/if}
</Card>
<style>
.sizes {
display: flex;
gap: 0.5rem;
margin-bottom: 1rem;
}
.sizes a {
padding: 0.25rem 0.75rem;
border: 1px solid var(--line);
border-radius: 999px;
text-decoration: none;
}
.sizes a.on {
border-color: var(--accent);
background: var(--accent);
color: var(--on-accent);
}
table {
border-collapse: collapse;
}
td {
padding: 0.125rem 1rem 0.125rem 0;
}
</style>
src/app.css
/* One small stylesheet for every example: light or dark with the system,
violet accent, neutrals tinted toward it. */
:root {
color-scheme: light dark;
--bg: light-dark(oklch(0.99 0.004 295), oklch(0.2 0.014 290));
--panel: light-dark(oklch(1 0 0), oklch(0.24 0.016 290));
--sunk: light-dark(oklch(0.965 0.008 295), oklch(0.17 0.012 290));
--line: light-dark(oklch(0.9 0.012 295), oklch(0.34 0.02 290));
--ink: light-dark(oklch(0.22 0.02 290), oklch(0.96 0.006 295));
--muted: light-dark(oklch(0.48 0.02 290), oklch(0.76 0.016 290));
--accent: light-dark(oklch(0.5 0.21 290), oklch(0.72 0.15 292));
--accent-strong: light-dark(oklch(0.43 0.2 290), oklch(0.8 0.12 292));
--accent-soft: light-dark(oklch(0.95 0.03 295), oklch(0.3 0.06 290));
--on-accent: light-dark(oklch(1 0 0), oklch(0.18 0.02 290));
--warn: light-dark(oklch(0.52 0.17 40), oklch(0.78 0.12 50));
--radius: 0.5rem;
--space: 1rem;
--mono: ui-monospace, "Cascadia Code", Menlo, monospace;
}
:root[data-theme="light"] {
color-scheme: light;
}
:root[data-theme="dark"] {
color-scheme: dark;
}
* {
box-sizing: border-box;
}
body {
max-width: 40rem;
margin: 0 auto;
padding: 1.5rem 1rem 3rem;
background: var(--bg);
color: var(--ink);
font: 400 1rem/1.6 system-ui, -apple-system, "Segoe UI", sans-serif;
-webkit-font-smoothing: antialiased;
}
/* Type */
h1 {
margin: 0 0 1rem;
font-size: clamp(1.5rem, 1.2rem + 1.5vw, 1.875rem);
line-height: 1.15;
letter-spacing: -0.02em;
text-wrap: balance;
}
h2 {
margin: 2rem 0 0.75rem;
font-size: 1.125rem;
letter-spacing: -0.01em;
}
h3 {
margin: 0 0 0.5rem;
font-size: 1rem;
}
p {
margin: 0 0 var(--space);
}
a {
color: var(--accent);
text-underline-offset: 0.2em;
}
a:hover {
color: var(--accent-strong);
}
:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
code {
padding: 0.1em 0.35em;
border-radius: 0.3em;
background: var(--sunk);
font: 0.875em var(--mono);
}
pre {
margin: 0 0 var(--space);
padding: 0.875rem 1rem;
overflow-x: auto;
border: 1px solid var(--line);
border-radius: var(--radius);
background: var(--sunk);
font: 0.875rem/1.55 var(--mono);
}
pre code {
padding: 0;
background: none;
}
.muted {
color: var(--muted);
}
/* Nav between routes: a header of links, the current one marked. */
header,
nav {
display: flex;
flex-wrap: wrap;
gap: 0.25rem 1.25rem;
margin: 0 0 1.5rem;
padding: 0 0 0.75rem;
border-bottom: 1px solid var(--line);
font-weight: 600;
}
header a,
nav a {
color: var(--muted);
text-decoration: none;
}
header a:hover,
nav a:hover,
header a[aria-current],
nav a[aria-current] {
color: var(--ink);
}
/* Forms: fields stack their label above, the button sits at the end of the row. */
form {
display: flex;
flex-wrap: wrap;
align-items: end;
gap: 1rem 0.75rem;
margin: 0 0 1.75rem;
}
/* A lone button with an action is a form of its own: it sits in the line. */
form:has(> button:only-child) {
display: inline-flex;
margin: 0;
}
label {
position: relative;
display: grid;
flex: 1 1 12rem;
gap: 0.375rem;
color: var(--muted);
font-size: 0.875rem;
font-weight: 600;
}
label:has(> input[type="checkbox"]) {
display: flex;
flex: 0 0 auto;
align-items: center;
align-self: center;
}
input,
select,
textarea {
width: 100%;
min-height: 2.75rem;
padding: 0.5rem 0.75rem;
border: 1px solid var(--line);
border-radius: var(--radius);
background: var(--panel);
color: var(--ink);
font: inherit;
font-size: 1rem;
font-weight: 400;
transition: border-color 0.15s, box-shadow 0.15s;
}
input:hover,
select:hover,
textarea:hover {
border-color: var(--muted);
}
input:focus,
select:focus,
textarea:focus {
border-color: var(--accent);
outline: none;
box-shadow: 0 0 0 3px var(--accent-soft);
}
input[type="checkbox"] {
width: 1.125rem;
min-height: 0;
accent-color: var(--accent);
}
input[type="file"] {
padding: 0.4rem;
}
[aria-invalid="true"] {
border-color: var(--warn);
}
/* A field's problem sits right under it, so the button beside the field stays put. */
label .problem {
position: absolute;
top: calc(100% + 0.25rem);
}
form:has(.problem) {
row-gap: 2.25rem;
margin-bottom: 2.75rem;
}
.problem {
display: block;
color: var(--warn);
font-size: 0.8125rem;
font-weight: 500;
}
/* Buttons */
button,
.button {
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 2.75rem;
padding: 0 1.125rem;
border: 1px solid var(--accent);
border-radius: var(--radius);
background: var(--accent);
color: var(--on-accent);
font: 600 0.9375rem/1 system-ui, sans-serif;
text-decoration: none;
white-space: nowrap;
cursor: pointer;
transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
button:hover,
.button:hover {
border-color: var(--accent-strong);
background: var(--accent-strong);
color: var(--on-accent);
}
button:active {
transform: translateY(1px);
}
button:disabled,
button[aria-busy="true"] {
opacity: 0.6;
cursor: progress;
transform: none;
}
button.quiet {
min-height: 2.25rem;
padding: 0 0.75rem;
border-color: var(--line);
background: var(--panel);
color: var(--ink);
font-weight: 500;
}
button.quiet:hover {
border-color: var(--accent);
background: var(--accent-soft);
color: var(--ink);
}
.bar {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
margin-top: 1rem;
}
/* Lists: plain ones keep their bullets; a .list is rows in a panel. */
ul,
ol {
margin: 0 0 var(--space);
padding-left: 1.25rem;
}
li + li {
margin-top: 0.25rem;
}
ul.list {
margin: 0 0 var(--space);
padding: 0;
overflow: hidden;
border: 1px solid var(--line);
border-radius: var(--radius);
background: var(--panel);
list-style: none;
}
label + ul.list {
margin-top: 0.75rem;
}
ul.list li {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 0.5rem 0.75rem;
margin: 0;
padding: 0.625rem 0.75rem 0.625rem 1rem;
}
ul.list li + li {
border-top: 1px solid var(--line);
}
/* An empty list says so, quietly, in the middle. */
ul.list li.muted:only-child {
justify-content: center;
padding: 1.5rem 1rem;
background: var(--sunk);
}
/* Tables */
table {
width: 100%;
margin: 0 0 var(--space);
border-collapse: collapse;
font-size: 0.9375rem;
}
th,
td {
padding: 0.5rem 0.75rem;
border-bottom: 1px solid var(--line);
text-align: left;
}
th {
color: var(--muted);
font-size: 0.8125rem;
font-weight: 600;
}
hr {
margin: 1.5rem 0;
border: 0;
border-top: 1px solid var(--line);
}
@media (prefers-reduced-motion: reduce) {
* {
transition: none !important;
animation: none !important;
}
}
src/app.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
%wisp.head%
</head>
<body>
%wisp.body%
</body>
</html>
How It Works
src/routes/+page.wisp shows the template blocks in one page. The --- block picks a Size from cx.query("size") and builds the fruit list.
{#each ... as (i, name)}with{:else}draws the table rows.{#snippet row(...)}defines a row once, and{@render row(name, i)}uses it.{#match size}with{:case ...}picks a message.{#await weather()}shows a placeholder while the slowasync fnruns, and{:then w}shows the result. The page does not wait for it.{@const total = ...}with{#if}and{:else if}shows one of three lines.class:on=${...}marks the current link.
src/components/Card.wisp is the component. {@props title, tone: &str = "plain", count: usize = 0} declares its inputs, and {@render children()} draws what is inside it.
Try it in the preview: switch between Empty, Few and Many.