nkondrashov/yii3-htmx 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nkondrashov/yii3-htmx

最新稳定版本:0.2

Composer 安装命令:

composer require nkondrashov/yii3-htmx

包简介

htmx extension for Yii Framework

关键字:

README 文档

README

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

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2024-02-24