From 312095e6b1b9838580e3f6422c3f90e71ffb5295 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Sun, 8 Sep 2024 19:14:05 +0100 Subject: [PATCH 1/2] livewire:script and livewire:assets --- README.md | 19 +++++++++++++++++++ src/Tags/Livewire.php | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/README.md b/README.md index fce2a66..235584f 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,25 @@ To include your Livewire component: ``` +### @script and @assets +Antlers versions of [@script](https://livewire.laravel.com/docs/javascript#executing-scripts) and [@assets](https://livewire.laravel.com/docs/javascript#loading-assets) are provided: + +```html + + {{ livewire:script }} + + {{ /livewire:script }} + +``` + +```html + + {{ livewire:assets }} + + {{ /livewire:assets }} + +``` + ### Blade or Antlers? Both! If creating a Livewire component, you need to render a template file diff --git a/src/Tags/Livewire.php b/src/Tags/Livewire.php index 57f0c52..5995732 100644 --- a/src/Tags/Livewire.php +++ b/src/Tags/Livewire.php @@ -86,4 +86,37 @@ public function scriptConfig(): string { return \Livewire\Mechanisms\FrontendAssets\FrontendAssets::scriptConfig(); } + + /** + * Antlers implementation of @assets - https://livewire.laravel.com/docs/javascript#loading-assets + * + * {{ livewire:assets }}....{{ /livewire:assets }} + */ + public function assets(): void + { + $html = (string) $this->parse(); + + $key = md5($html); + + \Livewire\store($this->context['__livewire'])->push('assets', $html, $key); + } + + /** + * Antlers implementation of @script - https://livewire.laravel.com/docs/javascript#executing-scripts + * + * {{ livewire:script }}...{{ /livewire:script }} + */ + public function script(): void + { + $html = trim((string) $this->parse()); + + $key = md5($html); + + if (in_array($key, \Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets::$alreadyRunAssetKeys)) { + // Skip it... + } else { + \Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets::$alreadyRunAssetKeys[] = $key; + \Livewire\store($this->context['__livewire'])->push('scripts', $html, $key); + } + } } From 71754e39562dd3500236c6ad53ee207b618fe6e1 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 9 Sep 2024 06:29:17 +0100 Subject: [PATCH 2/2] fix logic --- src/Tags/Livewire.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Tags/Livewire.php b/src/Tags/Livewire.php index 5995732..3418c07 100644 --- a/src/Tags/Livewire.php +++ b/src/Tags/Livewire.php @@ -98,7 +98,12 @@ public function assets(): void $key = md5($html); - \Livewire\store($this->context['__livewire'])->push('assets', $html, $key); + if (in_array($key, \Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets::$alreadyRunAssetKeys)) { + // Skip it... + } else { + \Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets::$alreadyRunAssetKeys[] = $key; + \Livewire\store($this->context['__livewire'])->push('assets', $html, $key); + } } /** @@ -112,11 +117,6 @@ public function script(): void $key = md5($html); - if (in_array($key, \Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets::$alreadyRunAssetKeys)) { - // Skip it... - } else { - \Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets::$alreadyRunAssetKeys[] = $key; - \Livewire\store($this->context['__livewire'])->push('scripts', $html, $key); - } + \Livewire\store($this->context['__livewire'])->push('scripts', $html, $key); } }