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..3418c07 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); + + 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); + } + } + + /** + * 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); + + \Livewire\store($this->context['__livewire'])->push('scripts', $html, $key); + } }