A simple Alerts class integrated with htmx and Alpine.js for CodeIgniter 4 framework.
MIT License
A simple Alerts class integrated with htmx and Alpine.js for CodeIgniter 4 framework.
composer require michalsn/codeigniter-htmx-alerts
You have to publish the config file first:
php spark alerts:publish
Now you can change:
Alerts key name used in views and session.
The default alert display time in milliseconds.
The array of message types, where array key is a CSS class and value is the title of the alert type.
Array keys are also used to determine the type of the alert we want to set, ie:
alerts()->set('success', 'Success message goes here.');
Wrapper id
name, used in the view file.
View files used by this library. You can change them to reflect the current style/theme you're using.
The default view files are designed to play along with Tabler theme.
In your main layout place the code (usually it will be just before the closing </body>
tag):
<?= alerts()->container(); ?>
That's it. You're ready to go. No matter if this is a htmx
request or traditional one, your alerts will be placed correctly every time.
You can add alerts in your controllers.
// success alert
alerts()->set('success', 'Success message');
// error message
alerts()->set('danger', 'Error message');
// custom display time - 1 sec (in milliseconds)
alerts()->set('success', 'Message', 1000);
You can also remove alerts by type.
// will remove all success alerts
alerts()->clear('success');
// will remove all alerts
alerts()->clear();