Skip to content

Displaying HTMX Blocks

Using a Block

The HTMX module offers a standard core block, HTMX Loader. This block is configured to react to an event. When triggered, the markup for an HTMX Block is obtained via a GET request. This markup replaces the HTMX Loader which was triggered.

In your custom markup

You can use a similar approach to that in HTMX Loader in your own code. Here is the relevant part of the render array:

$htmx = new HtmxAttribute();
$htmx->get(Url::fromRoute('htmx_blocks.view', ['block' => $htmxBlockId]]))
  ->select('#block-' . Html::getId($htmxBlockId))
  ->swap('outerHTML');

$build = [
   '#attributes' => $htmx->toArray(),
]

There are a variety of strategies available for hx-swap.