In a traditional CodeIgniter 4 application, setting up alerts is quite simple. We can simply write a few lines of code or use a dedicated library like codeigniter4-alerts.

Things get complicated, however, when we use htmx and want the alerts to interact with the way it works. Here a library dedicated to work with htmx can come to the rescue.

Installation

Installation via composer is very simple:

composer require michalsn/codeigniter-htmx-alerts

Next, we can add a container in which alerts will be displayed in our view (or main layout).

<?= alerts()->container(); ?>

Usually it will be just before the closing </body> tag.

Config

By default, the view files are build to work with Tabler theme. But you can simply change the view files and adjust them to the look you want.

To do so, you have to publish the config file.

php spark alerts:publish

After this, you can search for the $views property array in app/Config/Alerts.php file.

Usage

Setting an alert is simple as this:

alerts()->set('success', 'Success message goes here.');

You don’t have to worry if you’re dealing with htmx request or the traditional one. Alerts will be displayed automatically on the page.