Point at a card, or tab to it, to see where it comes from in the file; point at the code to find its card. This exact file compiles on the html target on every docs build.
home-example.mx
import ProductCard from "./tags/product-card.mx";
import { money } from "./format.ts";
import type { Product } from "./product.ts";
export interface Input {
products: Product[];
currency: string;
filter?: string;
emptyHtml?: string;
}
static const MAX_ITEMS = 3;
<const/shown=input.products.slice(0, MAX_ITEMS)/>
<const/cardAttrs={ currency: input.currency }/>
<let/selectedId=shown[0]?.id/>
<Cheapest/best products=shown/>
<define/Row|{ label, value }|>
<dt>${label}</dt>
<dd>${value}</dd>
</define>
<h1#store-title.title>${shown.length} products, one file</h1>
<input:q.search type=:search placeholder="Filter" required/>
<if=shown.length === 0>
<p.empty>$!{input.emptyHtml ?? "No products yet."}</p>
</if>
<else if=input.filter>
<p.hint>Filtered by ${input.filter}.</p>
</else>
<else>
<p.hint>Showing all ${shown.length} products.</p>
</else>
<ul.store>
<for|product, i| of=shown>
<li data-first=(i === 0) style={ "padding-left": `${i}rem` }>
<ProductCard product=product ...cardAttrs>
<@badge><span.badge>New</span></@badge>
<@price|amount|><b.sale>${money(amount, input.currency)}</b></@price>
</ProductCard>
</li>
</for>
</ul>
<dl.summary>
<Row label="Cheapest" value=best ? money(best.price, input.currency) : "—"/>
<Row label="Selected" value=selectedId ?? "none"/>
</dl>
footer.printed
p -- Concise mode: one tag per line, no closing tags.
Markup
A prop that holds markup is written as markup, inside the call. Pipes make it a function of its parameters.
from <@badge>, <@price|amount|>
in JSXbadge={<span className="badge">New</span>}
price={(amount) => (
<b className="sale">{money(amount)}</b>
)}
Name sugar
#id, .class and :name sit on the tag, where a selector would put them.
from #id .class, :name
in JSX<h1 id="store-title" className="title">
<input name="q" className="search" />
Control flow
Conditions and loops are tags with a body: no ternaries, no .map().
from <if> <else>, <for>
in JSX{shown.length === 0 ? (…) : filter ? (…) : (…)}
{shown.map((product, i) => (
<li key={product.id}>…</li>
))}
Attributes
A value is any expression, with no braces around it: an atom, a comparison, an object, a spread.
from :atom, name="value", boolean, name=(expr), style={…}, ...spread
in JSXtype="search" required
data-first={i === 0}
style={{ paddingLeft: `${i}rem` }}
{...cardAttrs}
Text
${} escapes what it prints. $!{} inserts markup, and you have to ask for it.
from ${}, $!{}
in JSX{shown.length} products, one file
dangerouslySetInnerHTML={{ __html: … }}
Logic
Values
static runs once per module, <const> on every render, and <let> is state.
from static, <const>, <let>
<define> names a fragment you call like a tag. A file in tags/ needs no import and can hand a value back.
from tags/, /var, <define>
The file
A typed module
Imports are TypeScript's. The exported Input is the type every caller is checked against.
from import, import type, Input
Concise mode, comments
Drop the angle brackets and indent instead. An HTML comment stays in the source.
from <!-- -->, concise mode