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:
htmx::redirecttriggers a full same-origin navigation viaHX-Redirect(sanitized against open redirects).redirect_externalemits an explicit303 See Otherredirect to anhttp://orhttps://URL.htmx::with_triggerattaches anHX-Triggerclient event header to a fragment.htmx::oob,htmx::oob_replace, andhtmx::oob_deletewrap sanitized out-of-band DOM swaps.htmx::is_htmxandhtmx::wants_jsoninspectHX-RequestandAcceptheaders.
§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-oobfragments, and signed flash cookies.
Macros§
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§
Constants§
- BUG_
REPORT_ JS - External bug-reporter script served at
/_slabs/vendor/slabs-bug-report.js. - 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§
- bug_
report_ widget - Render a toggleable, CSP-safe user bug reporter button and native
<dialog>widget. - bug_
report_ widget_ with_ label - Render a toggleable, CSP-safe user bug reporter widget with a custom trigger
button_label. - csrf_
token - Generate a domain-separated, expiring HMAC-signed CSRF token bound to
scope. - document
- Render a complete, CSP-friendly HTML5 page around
bodyusing compile-timeMarkup. - escape
- Escape
&,<,>,", and'into HTML entities for manually assembled strings. - flash_
banner - Render a Basecoat alert banner for an optional
Flashmessage. - json_
script - Serialize
valueas JSON safely embedded inside<script type="application/json" id="...">. - redirect_
external - Emit an explicit
303 See Otherredirect to an externalhttp://orhttps://URL. - render_
markdown - Render untrusted
CommonMarkand GitHub-flavored Markdown into safeMarkup. - 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.