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:

$build = [
   '#attributes' => [
          'hx-get' => Url::fromRoute('htmx_blocks.view', ['block' => $htmxBlockId]])->toString(),
          'hx-select' => '#block-' . Html::getId($htmxBlockId),
          'hx-swap' => 'outerHTML',
  ]
]

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