From 178e729d4191b39a42cc002be38e03933f7ce7b1 Mon Sep 17 00:00:00 2001 From: zunnu Date: Mon, 22 Jul 2024 19:16:45 +0300 Subject: [PATCH 1/3] Block rendering --- src/Controller/Component/HtmxComponent.php | 59 +++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/Controller/Component/HtmxComponent.php b/src/Controller/Component/HtmxComponent.php index 3f430a6..842cd78 100644 --- a/src/Controller/Component/HtmxComponent.php +++ b/src/Controller/Component/HtmxComponent.php @@ -18,6 +18,13 @@ class HtmxComponent extends Component */ protected array $_defaultConfig = []; + /** + * The name of the block. + * + * @var string|null + */ + protected ?string $block = null; + /** * List of triggers to use on request * @@ -39,6 +46,19 @@ class HtmxComponent extends Component */ private array $triggersAfterSwap = []; + /** + * Get the callbacks this class is interested in. + * + * @return array + */ + public function implementedEvents(): array + { + return [ + 'View.beforeRender' => 'beforeRender', + 'View.afterRender' => 'afterRender', + ]; + } + /** * Initialize properties. * @@ -54,13 +74,28 @@ public function initialize(array $config): void * * @return void */ - public function beforeRender(): void + public function beforeRender($event): void { if ($this->getController()->getRequest()->is('htmx')) { $this->prepare(); } } + /** + * afterRender callback. + * + * If setBlock is used this will render the set block if it exists + * + * @return void + */ + public function afterRender($event) + { + if(!empty($this->block) && $event->getSubject()->exists($this->block)) { + $block = $event->getSubject()->fetch($this->block); + $event->getSubject()->assign('content', $block); + } + } + /** * The current URL of the browser when the htmx request was made. * @@ -322,4 +357,26 @@ private function encodeTriggers(array $triggers): string return implode(',', array_keys($triggers)); } + + /** + * Set a specific block to render + * + * @param null|string $block Name of the block + */ + public function setBlock($block): static + { + $this->block = $block; + + return $this; + } + + /** + * Get the block that will be rendered + * + * @return null|string + */ + public function getBlock(): ?string + { + return $this->block; + } } From fcaaca2612d46bf5f49d1b8f9f395f15c1a50c07 Mon Sep 17 00:00:00 2001 From: zunnu Date: Mon, 22 Jul 2024 19:17:50 +0300 Subject: [PATCH 2/3] Update HtmxComponent.php --- src/Controller/Component/HtmxComponent.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Controller/Component/HtmxComponent.php b/src/Controller/Component/HtmxComponent.php index 842cd78..42a24a9 100644 --- a/src/Controller/Component/HtmxComponent.php +++ b/src/Controller/Component/HtmxComponent.php @@ -90,7 +90,7 @@ public function beforeRender($event): void */ public function afterRender($event) { - if(!empty($this->block) && $event->getSubject()->exists($this->block)) { + if (!empty($this->block) && $event->getSubject()->exists($this->block)) { $block = $event->getSubject()->fetch($this->block); $event->getSubject()->assign('content', $block); } @@ -324,7 +324,7 @@ public function clientRefresh(): ?Response * @param array $headers The headers that will be set * @return \Cake\Http\Response|null */ - public function stopPolling($content = '', array $headers = []): ?Response + public function stopPolling(string $content = '', array $headers = []): ?Response { $response = $this->getController()->getResponse(); @@ -360,10 +360,10 @@ private function encodeTriggers(array $triggers): string /** * Set a specific block to render - * - * @param null|string $block Name of the block + * + * @param string|null $block Name of the block */ - public function setBlock($block): static + public function setBlock(?string $block): static { $this->block = $block; @@ -372,8 +372,8 @@ public function setBlock($block): static /** * Get the block that will be rendered - * - * @return null|string + * + * @return string|null */ public function getBlock(): ?string { From d13f8f47e803e2ba748dff4da129d82219578cde Mon Sep 17 00:00:00 2001 From: Juhani Aronen Date: Mon, 22 Jul 2024 19:43:06 +0300 Subject: [PATCH 3/3] Add usage example --- README.md | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d43929e..516fa40 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ CakePHP integration for [htmx](https://htmx.org/). -Supported CakePHP Versions >= 4.x.x. 5.x support will be added later. +Supported CakePHP Versions >= [4.x](https://github.com/zunnu/cake-htmx/tree/4.x) and 5.x. ## Installing Using [Composer][composer] @@ -113,6 +113,123 @@ document.body.addEventListener('htmx:configRequest', (event) => { }) ``` +## Examples + +### Users index search functionality + +In this example, we will implement a search functionality for the users' index using Htmx to filter results dynamically. We will wrap our table body inside a [viewBlock](https://book.cakephp.org/5/en/views.html#using-view-blocks) called `usersTable`. When the page loads, we will render the `usersTable` [viewBlock](https://book.cakephp.org/5/en/views.html#using-view-blocks). + +```php +// Template/Users/index.php + +Form->control('search', [ + 'label' => false, + 'placeholder' => __('Search'), + 'type' => 'text', + 'required' => false, + 'class' => 'form-control input-text search', + 'value' => !empty($search) ? $search : '', + 'hx-get' => $this->Url->build(['controller' => 'Users', 'action' => 'index']), + 'hx-trigger' => "keyup changed delay:200ms", + 'hx-target' => "#search-results", + 'templates' => [ + 'inputContainer' => '
{{content}}
' + ] +]); ?> + + + + + + + + + + + + + + + start('usersTable'); ?> + + + + + + + + + + + end(); ?> + + fetch('usersTable'); ?> + +
id ?>name) ?>email) ?>modified ?>created ?> + Html->link('Edit', + [ + 'action' => 'edit', + $user->id + ], + [ + 'escape' => false + ] + ); ?> + Form->postLink('Delete', + [ + 'action' => 'delete', + $user->id + ], + [ + 'confirm' => __('Are you sure you want to delete user {0}?', $user->email), + 'escape' => false + ] + ); ?> +
+``` +In out controller we will check if the request is Htmx and if so then we will only render the `usersTable` [viewBlock](https://book.cakephp.org/5/en/views.html#using-view-blocks). + +```php +// src/Controller/UsersController.php + +public function index() +{ + $search = null; + $query = $this->Users->find('all'); + + if ($this->request->is('get')) { + if(!empty($this->request->getQueryParams())) { + $data = $this->request->getQueryParams(); + + if(isset($data['search'])) { + $data = $data['search']; + $conditions = [ + 'OR' => [ + 'Users.id' => (int)$data, + 'Users.name LIKE' => '%' . $data . '%', + 'Users.email LIKE' => '%' . $data . '%', + ], + ]; + $query = $query->where([$conditions]); + $search = $data; + } + } + } + + $users = $query->toArray(); + $this->set(compact('users', 'search')); + + if($this->getRequest()->is('htmx')) { + $this->viewBuilder()->disableAutoLayout(); + + // we will only render the usersTable viewblock + $this->Htmx->setBlock('usersTable'); + } +} +``` + + + ## License Licensed under [The MIT License][mit].