htmx in yf htmx
How the framework uses htmx: the asset, the request and response helpers of _class('htmx'), and the features built on them.
Options
| Option | Meaning |
|---|---|
asset('htmx') | Loads htmx (vendored, no CDN); widgets that need it load it themselves. |
_class('htmx') | Request facts (is_hx, req) and response helpers (_result(), _redirect(), _toast()...): see the entries of this chapter. |
X-CSRF-Token | A POST from htmx sends the session token in this header (hx-headers), checked by _class('csrf_guard')->check(). |
Example
nothing asked yet
Code
$hx = _class('htmx');
if ($hx->_wants_fragment() && ($_GET['hx'] ?? '') === 'time') {
return $hx->_result('<b>' . date('H:i:s') . '</b>, answered by the server as a fragment');
}
asset('htmx');
return '<button type="button" class="btn btn-primary" hx-get="' . _prepare_html(url('/@object/htmx/overview')) . '" hx-vals=\'{"hx": "time"}\' hx-target="#docs-ref-hx-time">Ask the server</button> '
. '<span id="docs-ref-hx-time">nothing asked yet</span>';Notes
- The pattern of every htmx page: the same URL answers an htmx request with a fragment (
_result()ends the request) and any other request with the whole page, so links work without JavaScript and bookmarks keep working. - Built on it: table2 paging, sort and filter, the refresh control, the admin confirm, HX-Redirect / HX-Refresh, boosted links, toasts.
- Helpers that send a header end the request unless
EXIT_AFTER_HEADERis false; tests setSEND_HEADERS = falseto record the lines insent_headers.
See also: htmx/request, htmx/fragment, demo:htmx/table, demo:htmx/inline_edit, api:yf_htmx