PHPackages                             magehx/htmx-actions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. magehx/htmx-actions

ActiveMagento2-module[Utility &amp; Helpers](/categories/utility)

magehx/htmx-actions
===================

Magento 2 module that provides a base controller to manage HTMX requests.

1.0.0(8mo ago)623721MITPHP

Since Aug 31Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/magehx/htmx-actions)[ Packagist](https://packagist.org/packages/magehx/htmx-actions)[ RSS](/packages/magehx-htmx-actions/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (1)

MageHx\_HtmxActions
===================

[](#magehx_htmxactions)

A Magento 2 module that enhances HTMX integration by providing structured controllers, events, view models, services, and enums to streamline dynamic HTML responses.

An HTMX request expects an HTML snippet in response, which will be dynamically swapped into the frontend page.

This module provides many utilities to ease this process.

It offers a convenient base for handling common HTMX response scenarios, allowing you to return HTML fragments efficiently and consistently.

---

✨ Features
----------

[](#-features)

- **Abstract Controller**: Simplifies handling HTMX requests with a base controller.
- **Event Dispatching**: Fires events during the HTMX request lifecycle for extensibility.
- **View Models**: Provides view models to assist in rendering HTMX attributes in templates.
- **Header Management Service**: Offers a service to manage HTMX-specific response headers.
- **Enumerations**: Defines enums for HTMX attributes and headers to ensure consistency.
- **HTMX Attribute Renderers**: Includes traits for rendering HTMX attributes in templates.

---

📦 Installation
--------------

[](#-installation)

Install the module via Composer:

```
composer require magehx/htmx-actions
```

---

🔧 Usage
-------

[](#-usage)

### Abstract Controller

[](#abstract-controller)

Extend the `MageHx\HtmxActions\Controller\HtmxAction` to handle HTMX requests:

- `getBlockResponse()` - This allows to retrieve a block response from the given layout handles.
- `getMultiBlockResponse()` - This allows to retrieve combined blocks response from the given layout handles.
- `getEmptyResponse()` - Gives empty response. Useful when you decided to not swap the content in the frontend.

```
use MageHx\HtmxActions\Controller\HtmxAction;

class Example extends HtmxAction
{
    public function execute(): \Magento\Framework\Controller\ResultInterface
    {
        return $this->getBlockResponse('your.block.name', ['your_layout_handle']);
    }
}
```

#### Event Dispatching

[](#event-dispatching)

The controller dispatches useful events during the HTMX request lifecycle:

- `hxactions_block_response_before`: Fires before preparing the block response. Useful to add more layout handles or change the block itself.
- `hxactions_block_response_after`: Fires after preparing the block response. Useful to change the response.
- `hxactions_multi_blocks_response_before`: Fires before preparing the multi block response.
- `hxactions_multi_blocks_response_after`: Fires after preparing the multi block response.

Observers can be created to listen to these events for custom logic.

---

### View Models

[](#view-models)

`MageHx\HtmxActions\ViewModel\HxAttributesRenderer` provides many convenient ways to render HTMX attributes on HTML elements

```
