Skip to content

nkondrashov/yii3-htmx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yii3 Framework htmx simple extension


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

For license information check the LICENSE-file.

Installation

  1. The preferred way to install this extension is through composer.
php composer.phar require --prefer-dist nkondrashov/yii3-htmx
  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.

General usage

Examples

Simple:

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

$htmx = HTMX::make($tag)
            ->request(Yiisoft\Http\Method::DELETE, '/item/delete/' . $todo->id)
            ->triggerCustomEventAfterRequest('someCustomEvent')
            ->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)
            ->triggerCustomEventAfterRequest('someCustomEvent')
            ->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)
            ->triggerCustomEventAfterRequest('someCustomEvent')
            ->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');
?>

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 ¯_(ツ)_/¯