nkondrashov/yii3-htmx

htmx extension for Yii Framework

0.2 2024-02-24 23:10 UTC

This package is auto-updated.

Last update: 2024-04-24 23:38:36 UTC


README

68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667 htmx_logo.1.png

Yii3 Framework htmx simple extension


This Yii Framework extension encapsulates basic functions [htmx] and makes using in Yii applications extremely easy.

You can see examples in yii3-demo-htmx application.

Installation

  1. The preferred way to install this extension is through composer.
php composer.phar require nkondrashov/yii3-htmx "^0.2"
  1. Add HTMXMiddleware.php to router.
->middleware(HTMXMiddleware::class)
  1. Register asset in main layout or AppAsset
$assetManager->register(HTMXAsset::class);

Warning!

  1. Add to <body> tag attribute hx-headers='{"X-CSRF-Token":"<?=$csrf; ?>"}' for success post requests. Like this:
<body hx-headers='{"X-CSRF-Token":"<?=$csrf; ?>"}'>

Install assets

Using the npm-asset package manager.

Run the following command at the root directory of your application.

npm i htmx.org@1.9.10

General usage

Examples

Simple:

<?php
$tag = Yiisoft\Html\Html::button('[ X ]']);

$htmx = HTMX::make($tag)
            ->request(Yiisoft\Http\Method::DELETE, '/item/delete/' . $todo->id)
            ->setSwap('none')
            ->runOnClick();

if (!$todo->is_complete) {
    $htmx->addConfirm('Are you sure?');
}

echo $htmx;
 ?>
<?= HTMX::make(Yiisoft\Html\Html::tag('div'))
        ->request(Yiisoft\Http\Method::GET, '/item/index')
        ->runOnCustomEvent('someCustomEvent', 'someCustomEvent2')
        ->runOnLoad();?>

More native:

<?php
$tag = Yiisoft\Html\Html::button('[ X ]']);

$htmx = HTMX::make($tag)
            ->request(Yiisoft\Http\Method::DELETE, '/item/delete/' . $todo->id)
            ->addTriggers('click')
            ->setSwap('none');

if (!$todo->is_complete) {
    $htmx->addConfirm('Are you sure?');
}

echo $htmx;
 ?>
<?= HTMX::make(Yiisoft\Html\Html::tag('div'))
        ->request(Yiisoft\Http\Method::GET, '/item/index')
        ->addTriggers('load','someCustomEvent from:body', 'someCustomEvent2 from:body');
?>

Max native:

<?php
$tag = Yiisoft\Html\Html::button('[ X ]']);

$htmx = HTMX::make($tag)
            ->setHx('delete', '/item/delete/' . $todo->id)
            ->setHx('trigger', 'click')
            ->setHx('swap', 'none');

if (!$todo->is_complete) {
    $htmx->setHx('conf', 'Are you sure?');
}

echo $htmx;
 ?>
<?= HTMX::make(Yiisoft\Html\Html::tag('div'))
        ->setHx('get', '/item/index')
        ->setHx('trigger', 'load, someCustomEvent from:body, someCustomEvent2 from:body');
?>

Send htmx headers in response:

//Definition:
...
    public function __construct(private HTMXHeaderManager $headerManager)
    {}
...

//In code:
$this->headerManager->triggerCustomEventAfterSwap('updateList');
//or native:
$this->headerManager->sendHXHeader('Trigger', 'updateList');

//Detect htmx request:
if ($this->headerManager->isHtmxRequest()) {
    //Example: This request without htmx header
}

//Get data from header:
$value = $this->headerManager->getRequestHeader('Hx-Trigger-Name');

Support the Yii3 project

Open Collective

Follow Yii3 updates

Official website Twitter Telegram Facebook Slack

License

The Yii Framework htmx Extension is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

This package maintained by Me ¯_(ツ)_/¯