fjkit cheatsheet

Every macro the kit ships, grouped by what it is for. Each row answers the question an index gets asked most often: does this take a block, and what goes inside it. The Components page is where you see them running; this is where you look one up.

How to read this

What is on a row, and two rules that apply to every one of them.

A Jinja macro is called in one of two ways, and the name does not tell you which. You print a macro that has nothing inside it. You call a macro that wraps something with {% call %}, and Jinja hands whatever sits between that and {% endcall %} to the macro to place. Jinja calls that the caller block, and every macro below that wants one carries a badge saying what goes in it.

{% from "ui/data.html" import badge, card %}

{# printed — this macro takes no block #}
{{ badge("Doing", variant="info") }}

{# a block — the card renders it as its body #}
{% call card("Tasks", "Everything open right now.") %}
  <p>Anything that belongs inside the card.</p>
{% endcall %}

What is on a row

The signature

the call, with every parameter and its default. The heading above each list is the file you import it from

A block badge

on the right of a row, and only on the rows that have one. Call this macro with {% call %}, and the badge says what goes inside. No badge means print it with {{ }}

One line under it

what the signature does not say. Absent when the signature says all of it

Two rules, everywhere

Extra keywords

anything you pass that is not in the signature becomes an attribute, with underscores turned into dashes: hx_post="/tasks" arrives as hx-post="/tasks". That is every hx-*, aria-* and data-*, on every macro

Roles, not colours

variant, tone and category name a role — success, destructive, muted — never a colour. The colour is the token layer's, so a rebrand moves it once


Buttons and icons

The two smallest things, and the plumbing every other macro forwards through.

ui/button.html

button(label, variant="", size="", type="button", href=none, icon_name=none, icon_end=false, disabled=false)

Passing href renders an <a> that still looks and behaves like a button.

button_group(gap=2, orientation="horizontal")

Spacing only. Each button keeps its own variant.

ui/icon.html

icon(name, size=16, cls="")

1,767 Lucide names. An unknown name raises at render time rather than drawing nothing.

ui/attrs.html

attrs(mapping)

An app never calls this. Every macro ends with **kwargs and runs it through here, which is how hx_post="/tasks" reaches the markup as hx-post="/tasks".

ui/swap.html

morph_scripts()

Loads the idiomorph extension, which adds the morph:* swap styles. The reply patches the DOM instead of replacing it, so focus, scroll position, an open details and a ticked box survive a sort. Two halves — the element whose swaps should morph declares hx-ext="morph", and the swap itself is swap="morph:outerHTML". Per page, never the shell.

sse_scripts()

Loads htmx's sse extension, so a reply can arrive in pieces over one connection: hx-ext="sse" and sse-connect on the container, sse-swap on each element an event fills, sse-close naming the event that ends it. The server side is templates.events(), which always sends that event — without it the extension reconnects forever. Per page, never the shell.


Layout and page structure

Everything here takes a block, because a layout macro is a wrapper by definition. Reach for these before any utility class — that is the whole point of the closed vocabulary.

ui/layout.html

stack(gap=4, align=none)

Vertical rhythm. The default gap for a page's bands.

row(gap=3, align="center", justify=none, wrap=true)

Horizontal, and it wraps by default so a narrow screen never clips.

grid(cols=3, gap=4)

cols is 2, 3 or 4. The breakpoints belong to the macro; a caller that picks its own is how a layout vocabulary turns back into utility classes.

split(aside="md", gap=6)

Called as {% call(slot) split() %}, and the macro renders the block twice — once with slot="main", once with slot="aside".

centered(width="sm", gap=6)

The width cap the rest of this file cannot express. width is xs, sm, md, lg, xl or prose — a sign-in card, a settings form, a page of prose. It centres horizontally only.

fill(justify="center", align=none, gap=6)

The other axis. It takes the height the shell already gave main and places the children in it, so justify reads vertically here — center for a sign-in card, end for a footer that sits on the fold. Nest a centered inside it when the page wants both.

page_header(title, description=none)

Called without a block it is just the title and the description. The block is the actions slot, so buttons stay level with the title at every width.

section(title=none, description=none, gap=4)

One level below page_header. Use it when a page has more than one topic on it.

divider()

A hairline on the border token, so it survives both colour schemes. Prefer a section heading where the split has a name.

ui/sidebar.html

sidebar(id="sidebar", label="Main", side="left", open=true, header=none, footer=none)

Fill the shell's sidebar block with this, or its nav block with nav_links — not both.

sidebar_group(title=none)

sidebar_link(request, route, label, icon_name=none)

route is a route name, not a URL. The active state is is_active's, so a link cannot disagree with the page it is on.

sidebar_item(label, href, active=false, icon_name=none, lead=none)

For a URL that is not one route, such as the same route with a different parameter per row. lead puts a badge in a fixed-width cell so labels align.

sidebar_submenu(label, icon_name=none, open=false)

sidebar_trigger(target="sidebar", label="Toggle navigation")

The shell already renders one in the header when a sidebar is present.


Data display

What a row, a figure or a list of things looks like. Build the list in Python — a template that zips two lists is doing the router's job.

ui/data.html

badge(label, variant="")

variant is a role: success, warning, destructive, info, secondary, outline.

card(title=none, description=none, size="", actions=none, padded=true)

padded=false hands the body to the card's own edge — what a table or a code block needs.

stat(label, value, hint=none, icon_name=none, tone=none)

One figure. Several of them are metric_group.

metric_group(items, cols=3)

items is a list of (label, value) pairs.

progress(value, label=none, id=none)

value is 0–100. id makes the bar addressable, which is what form(progress=…) needs to move it during an upload.

empty_state(title, description=none, icon_name="sparkle")

table renders this for you when rows is empty — pass empty_title and empty_description instead of writing the branch.

bullet_list(tone="muted")

list_item()

item_list()

description_list(layout="rows")

layout: rows | stacked | inline. inline is a few short facts on one wrapping line, in small type.

description_item(term, value=none)

A native <dl>. A screen reader announces the value as the definition of its term. item_list is a list of things; this is a list of facts about one.

item(title, description=none, icon_name=none, actions=none, href=none, clamp=true)

clamp=false lets a description run past two lines — what a definition needs and a feed does not.

avatar(name, src=none, size="", initials=none, badge_tone=none, badge_icon=none)

The macro derives the initials from name unless you override them.

avatar_group(overflow=none, label=none)

code_block(source, label=none, wrap=false)

Carries the scroll region and its focusability, so a keyboard can reach a listing that overflows.

caption(text=none, size="sm", tone="muted", icon_name=none)

A muted line that stands on its own. card, section and page_header each render a description already, but those are bound to a heading.

link(label, href)

kbd(keys)

A key or a chord.

ui/table.html

table(columns, rows=none, empty_title="Nothing here", empty_description=none, empty_icon="list", target=none, swap="outerHTML", push_url=true, select_name="selected", select_label="Select all rows", resizable=false)

columns is a list of dicts — label, and optionally align, width, sort, sort_url, select or group. Passing rows lets the macro own the empty case; pass rows=none to always render the body yourself. A column with a sort_url becomes a sortable header, and target turns those links into htmx swaps. resizable=true puts a drag handle on each labelled column and needs an id=, the key it stores the widths under. Columns sharing a group get one heading above them in a second header row.

cell(value=none, tone=none, numeric=false, align=none)

Pass value for text, or omit it and use a block for markup. tone is a closed lookup, so you cannot pass a colour through.

row_actions()

The trailing cell, right-aligned and tight.

select_cell(value, name="selected", checked=false, label=none)

The row half of a {"select": true} column. An ordinary checkbox with an ordinary name, so the selection posts as selected=3&selected=7. Give it a label: an id is not one.

select_count(name="selected", label="{n} selected", zero="None selected")

The live readout of how many rows are picked. You write both strings in the markup, so a translated page translates them. Pass zero=none and it hides while nothing is selected.

select_scripts()

Loads js/select.js, which ticks the column from the header box, shows the partial state and tints the picked rows. Per page, never the shell.

resize_scripts()

Loads js/resize.js, which shows the handles of a resizable table, follows a drag or the arrow keys, and keeps the widths in localStorage. Home or a double-click resets a column. Per page, never the shell.

page_size(url, per_page, options=(10, 25, 50, 100), param="per_page", keep=none, target=none, label="Rows per page", apply_label="Apply")

A GET form, because a select on its own cannot act on a change. url must carry no query string — a native submit throws it away — so pass the filters in keep as hidden fields. Never the page number. With a target the select applies itself, and the submit button renders only without scripting.

pagination(page, pages, url, param="page", total=none, per_page=none, target=none, swap="outerHTML", push_url=true, window=1, label="Pagination")

url is the list's address without a page parameter; the macro appends one. Renders nothing when there is one page or fewer. Give total and per_page together for the "76–100 of 210" line.


Forms

Every field takes name, label, hint, error and id. Pass error and the field draws the message, the red ring and the aria-describedby wiring together.

ui/form.html

form(action=none, method="post", target=none, swap="outerHTML", reset_on_success=false, card=true, encoding="urlencoded", progress=none)

target and swap are the htmx pair. encoding="json" posts JSON, and then the page needs form_scripts(); encoding="multipart" is what an upload needs, and it is written for the htmx path and the native one because a browser with scripting off submits this form too. progress is a selector for a progress() bar — htmx reports what it has sent and the macro writes the number into all three places a bar carries it.

form_scripts()

Per page, not in the shell — this is what encoding="json" needs.

field_row(template="two", gap=3)

Two or three fields on one line, collapsing to one column when narrow. template="inline" gives each short field a fixed 16rem cell instead.

fieldset(legend=none, hint=none)

A named group inside a form, with the legend the group needs.

text_field(name, label=none, value="", placeholder="", type="text", required=false, hint=none, error=none, id=none)

type is where dates live: date, time, datetime-local, month and week are native inputs, and there is no date_field because a macro around one would add a name and nothing else. min/max/step pass through as attributes. Not file — an upload cannot be restored from value after a 422.

textarea_field(name, label=none, value="", placeholder="", rows=none, required=false, hint=none, error=none, id=none)

file_field(name, label=none, accept=none, multiple=false, required=false, hint=none, error=none, id=none)

No value parameter, because a browser will not let a page refill a file input. A 422 redraw comes back with this box empty and every other field still typed in. The form needs encoding="multipart" or the route gets the file's name as a string.

select_field(name, label=none, options=(), selected=none, id=none, blank=none, hint=none, error=none)

options is a list of (value, label) pairs.

checkbox_field(name, label=none, checked=false, value="on", hint=none, error=none, id=none)

switch_field(name, label=none, checked=false, value="on", hint=none, error=none, id=none)

radio_group(name, label=none, options=(), selected=none, hint=none, error=none, id=none)

range_field(name, label=none, value=50, min=0, max=100, step=1, hint=none, error=none, id=none, output=false)

output=true shows the live value beside the track, from the control itself rather than from a second source.

input_group_field(name, label=none, value="", placeholder="", type="text", start=none, end=none, required=false, hint=none, error=none, id=none, revealable=false, reveal_show="Show", reveal_hide="Hide")

start and end are the affixes — a currency symbol, a unit, a button. revealable=true adds the Show/Hide toggle a password field wants, and then the page needs reveal_scripts().

reveal_scripts()

Per page, not in the shell — this is what revealable=true needs. The listener is on document, so the button still works in a panel a 422 swapped in.



Overlays and disclosure

Anything that opens. Basecoat owns the open/close behaviour and the shell already loads it, so these macros carry the aria contract and nothing else.

ui/overlay.html

popover(id, label, variant="outline", size="", side="bottom", align="center", width="lg", icon_name=none)

Free markup in a floating panel. A menu of choices is dropdown_menu.

dropdown_menu(id, label, variant="outline", size="", side="bottom", align="start", width="default", icon_name=none)

menu_item(label, shortcut=none, variant="", disabled=false, checked=none, radio=false, href=none)

checked and radio give the item the matching aria role.

menu_group(heading=none, id=none)

menu_separator()

select_menu(name, options=(), selected=none, id=none, placeholder="Select…", width="lg", label=none, multiple=false, close_on_select=false, visible_label=none, hint=none, error=none)

A styled select that still posts a field. multiple=true needs multiselect_scripts() on the page. label is an aria-label and draws nothing; visible_label makes it a field, with the wrapper, the label and the message line every other field has.

combobox(name, options=(), selected=none, id=none, placeholder="Select…", empty="No results found.", label=none, multiple=false, close_on_select=false, visible_label=none, hint=none, error=none, search=none, search_param="q", search_delay=250)

select_menu with a filter box. Same multiple=true rule, same visible_label. search=<url> moves the filter to the server. The route receives ?q=<typed> and answers with option rows only. To act on a pick, listen for change on the root — hx_trigger="change target:#<id>" — because that is where Basecoat dispatches it.

multiselect_scripts()

Per page. Without it, multiple=true posts one JSON string instead of repeated fields.

drawer(id, title=none, description=none, side="bottom", footer=none, dismissible=true)

drawer_trigger(label, target, variant="outline", size="", icon_name=none)

command(id, placeholder="Type a command or search…", empty="No results found.", label="Command menu", dialog=false, bordered=true)

dialog=true makes it the overlay palette rather than an inline list.

command_group(heading=none, id=none)

command_item(label, keywords=none, icon_name=none, shortcut=none, disabled=false, filter=none, href=none)

keywords is what the filter box matches on beyond the label.

ui/disclosure.html

collapsible(summary, open=false, icon_name=none)

One section that opens. It is a <details>, so it works with scripting off.

accordion(multiple=false, label=none)

multiple=false closes the others when one opens.

tooltip(text, side="top", align="center")

A CSS ::after, so there is no second element and no script. The text is not in the accessibility tree — a tooltip is a hint, never the only label, and it cannot contain markup.


Feedback

What the interface says back. The variant decides the colour and, for an alert, whether a screen reader interrupts.

ui/feedback.html

spinner(size="default", tone="muted", label=none, indicator=false)

indicator=true makes it an htmx indicator: hidden until the request it belongs to is in flight.

alert(title, body=none, variant="", icon_name=none)

variant="destructive" gets role="alert", which interrupts a screen reader. Everything else gets role="status", which waits for a pause.

skeleton(shape="text", width="full", lines=1, label=none)

shape is text, heading, control, avatar or block.

dialog(id, title=none, description=none, size="default", footer=none, dismissible=true)

A native <dialog>. The footer is a slot rather than a block, because the body already is one.

toaster(align="end")

One per page, in the shell's toasts block. Server-sent toasts arrive through HX-Trigger.

toast(title, description=none, category="info", duration=none, action_label=none, action_href=none)


The shell

Blocks, not macros — the one file you extend rather than import.

ui/shell.html carries the <head>, the theme flash-guard, the asset links and the header/main/footer skeleton. An app extends it once, in its own base.html, and fills the blocks it needs. Fill sidebar or nav, never both. The shell lays the header out differently for each, and filling both gives you two navigations.

{% extends "ui/shell.html" %}

{% block site_title %}Acme{% endblock %}
{% block sidebar %}{# your rail #}{% endblock %}
{% block content %}{# the page #}{% endblock %}

ui/shell.html

site_title

The product name, after the page title in <title>.

title

This page's title.

lang

The <html lang> value.

brand

The mark in the header. Usually one brand() call.

nav

Header navigation. Fill this OR sidebar, never both.

sidebar

The rail. Fill this OR nav, never both.

header

The whole header bar, when the pieces above are not enough. The default is pinned to the top; override this block for a header that stays in the flow.

header_actions

The right-hand end of the header. Defaults to theme_toggle() under color_scheme="system", and to nothing under "light" or "dark".

sidebar_trigger

The button that opens the rail on a narrow screen.

content

The page itself.

footer

Footer text.

footer_wrapper

The footer element around it.

stylesheets

Your own stylesheet — token overrides and element typography.

head

Anything else in <head>: meta tags, og: tags.

toasts

Where toaster() goes. The shell renders one by default.

scripts

Your scripts, at the end of <body>. Use defer — htmx and Basecoat are deferred too, and a plain script here would run before either.


Globals

In every template already — no import, no context variable.

build_environment()

url_for(request, name, **params)

A route name to a URL. Names, not paths, so a moved route updates every link.

is_active(request, name)

True when the request is for that route. What sidebar_link marks with.

fjkit_static(path)

A URL under the mounted static tree, stamped with ?v=<mtime> so a browser cannot serve a stale stylesheet against current markup.

app_static(path)

A URL under FjkitConfig.static_dir — the app's own scripts and images — stamped the same way. Defined only when static_dir is set. Use it instead of url_for(request, "static", …), whose URL has no stamp.

fjkit_version

The versions in the footer: fjkit, fastapi, jinja2, basecoat, htmx.

fjkit_color_scheme

What FjkitConfig.color_scheme renders: .prepaint_script, .html_class and .toggle.

fjkit_icon_path(name)

The raw SVG path data for a Lucide name. icon() is what a template calls.


htmx attributes

Not fjkit's, but passed to fjkit macros — so they belong next to the index.

Every one of these reaches the markup as an extra keyword on a macro: {{ button("Refresh", hx_get="/tasks/board", hx_target="#board") }}. Underscores become dashes on the way through, and that is the whole integration. No macro has an htmx-specific parameter, and none needs one. The full reference is htmx.org/reference; what follows is what a fjkit page uses.

Which request

The verb and the URL.

hx-get="/tasks" hx-post="/tasks" hx-put="/tasks/5" hx-patch="/tasks/5" hx-delete="/tasks/5"

Fires the request. The response is HTML, not JSON — that is the whole difference from fetch().

Where the answer goes

Which element is replaced, and how.

hx-target="#board" a CSS selector hx-target="closest tr" the nearest ancestor hx-target="this" the element itself hx-target="next .row" the one after it

Defaults to the element that fired the request.

hx-swap="outerHTML" replace the target hx-swap="innerHTML" replace what is inside it hx-swap="beforeend" append (afterbegin prepends) hx-swap="afterend" insert after (beforebegin: before) hx-swap="delete" remove it (none: change nothing)

Modifiers attach to the same string: swap:200ms, settle:100ms, scroll:top.

When it fires

One attribute, and each form is a different job.

hx-trigger="click" hx-trigger="submit" hx-trigger="change"

The defaults — a button clicks, a form submits, an input changes. Write them out only when you are adding a modifier.

hx-trigger="keyup changed delay:400ms"

Search as you type. changed skips the request when the value did not move; delay debounces the keystrokes.

hx-trigger="revealed"

Lazy load: fires when the element scrolls into view. It tests that with getBoundingClientRect, so it is wrong for anything inside an overflow container or hidden by display:none — a hidden element reports an all-zero rect and counts as visible.

hx-trigger="intersect once" hx-trigger="intersect threshold:0.5"

Lazy load through an IntersectionObserver, which reports a hidden element as not intersecting. This is the one that works in a scroll container. Without once it fires every time the element becomes visible again, scrolling included, which is why tab_panel(lazy=) does not use it; root: and threshold: take the observer's own options.

hx-trigger="load"

Fires once, as soon as the element is inserted.

hx-trigger="every 2s"

Polling. Stop it by returning markup without the attribute.

hx-trigger="click from:body"

Listens somewhere else — a keyboard shortcut, a click outside a menu.

What else goes with it

Extra data, confirmation, history.

hx-vals='{"id": 3}'

Values sent with the request, on top of the form fields.

hx-include="#filters"

Other fields to send — a filter bar that sits outside the form.

hx-confirm="Delete this task?"

A native confirm before the request fires.

hx-indicator="#spinner"

That element gets the htmx-request class while the request is in flight. spinner(indicator=true) is already shaped for it.

hx-push-url="true"

Puts the URL in the address bar, so Back works.

hx-swap-oob="true"

Written in a response, not on the trigger: swaps a second element by id, somewhere else on the page.

Headers the route reads and writes

The half of htmx that lives in Python rather than in a template.

HX-Request: true (request)

On every htmx request. How one route answers a navigation with a page and a swap with a fragment.

HX-Trigger: {"toast": {…}} (response)

Fires a client event. This is how a toast raised in a route reaches the toaster in the shell.

HX-Redirect: /tasks (response) HX-Refresh: true (response)

Full-page navigation, asked for by a fragment response.

HX-Retarget: #errors (response)

Changes the target from the server — an error that belongs somewhere other than where the success would have gone.


When it is not here

The answer is a parameter, a keyword, or an issue — in that order.

When a macro is missing

check the parameters

most requests are a keyword that already exists — size, tone, padded, align

pass it through

any extra keyword becomes an attribute, which covers every hx-*, aria-* and data-*

fjkit eject badge

take that one macro into your app to edit; every other macro in its file is re-exported from the kit and keeps its fixes

open an issue

a blocked class names the component that does not exist yet; that is the report