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, CSP-friendly HTML5 page with zero inline <style> or inline <script> elements. Native view transitions (@view-transition { navigation: auto; }) ship inside /_slabs/vendor/basecoat.min.css, and service-worker registration loads externally via /_slabs/vendor/sw-register.js alongside Basecoat UI, HTMX 2 (with SSE and Idiomorph 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 domain-separated, expiring HMAC-signed token bound to a session scope and verify_csrf to verify 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.
SW_REGISTER_JS
External service-worker registration script served at /_slabs/vendor/sw-register.js.

Traits§

Render
Represents a type that can be rendered as HTML.

Functions§

csrf_token
Generate a domain-separated, expiring HMAC-signed CSRF token bound to scope.
document
Render a complete, CSP-friendly 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.
json_script
Serialize value as JSON safely embedded inside <script type="application/json" id="...">.
redirect_external
Emit an explicit 303 See Other redirect to an external http:// or https:// URL.
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.