Skip to main content

Crate slabs_html

Crate slabs_html 

Source
Expand description

Compile-time HTML rendering, document layout, HTMX helpers, and CSRF tokens for Slabs.

This crate re-exports html! (aliased as maud! in slabs::prelude) so handlers build auto-escaped HTML at compile time with zero runtime template engine.

§Document layout

document renders a complete HTML5 page with native view transitions (@view-transition { navigation: auto; }), vendored Basecoat UI styles and scripts (/_slabs/vendor/basecoat.min.css and /_slabs/vendor/basecoat.min.js), HTMX 2 with the SSE and Idiomorph extensions (hx-ext="morph" on <body>), and Alpine.js 3. Pass app-compiled Tailwind CSS v4 stylesheets (for example &ctx.assets.stylesheet_urls()) and module scripts via the stylesheets and scripts arguments. All frontend assets are self-hosted with zero CDN or Node runtime requirement.

§HTMX helpers

The htmx module emits standard HX-* headers and hx-swap-oob fragments:

§CSRF protection

Use csrf_token to mint a signed token bound to a session scope and verify_csrf to check submitted tokens.

§Usage

use axum::http::HeaderMap;
use axum::response::IntoResponse;
use slabs_html::{document, html, htmx};

fn render_notice(headers: &HeaderMap, styles: &[String]) -> axum::response::Response {
    let fragment = html! {
        p id="notice" { "Saved." }
    };

    if htmx::is_htmx(headers) {
        return htmx::oob_replace("notice", fragment).into_response();
    }

    document("Notice", styles, &[], fragment).into_response()
}

Modules§

htmx
Standard HTMX 2 helpers for HX-* headers, hx-swap-oob fragments, and signed flash cookies.

Macros§

html

Structs§

Flash
One-shot feedback message carried across a redirect in a signed cookie.
PreEscaped
A wrapper that renders the inner value without escaping.

Enums§

FlashKind
Severity level for a one-shot Flash banner message.

Constants§

DOCTYPE
The literal string <!DOCTYPE html>.
FLASH_COOKIE_NAME
Cookie name used to store one-shot signed flash messages.

Traits§

Render
Represents a type that can be rendered as HTML.

Functions§

csrf_token
Generate an HMAC-signed CSRF token bound to scope (such as a session ID or "anon").
document
Render a complete HTML5 page around body using compile-time Markup.
escape
Escape &, <, >, ", and ' into HTML entities for manually assembled strings.
flash_banner
Render a Basecoat alert banner for an optional Flash message.
render_markdown
Render untrusted CommonMark and GitHub-flavored Markdown into safe Markup.
verify_csrf
Verify a submitted CSRF token against scope.

Type Aliases§

Markup
A block of markup is a string that does not need to be escaped.