Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit cfe5981

Browse files
Merge pull request #70 from ryanmitchell/feature/assets-directive
livewire:script and livewire:assets
2 parents 580f26d + 71754e3 commit cfe5981

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ To include your Livewire component:
108108
</body>
109109
```
110110
111+
### @script and @assets
112+
Antlers versions of [@script](https://livewire.laravel.com/docs/javascript#executing-scripts) and [@assets](https://livewire.laravel.com/docs/javascript#loading-assets) are provided:
113+
114+
```html
115+
<body>
116+
{{ livewire:script }}
117+
<script>console.log('hello')</script>
118+
{{ /livewire:script }}
119+
</body>
120+
```
121+
122+
```html
123+
<body>
124+
{{ livewire:assets }}
125+
<script src="some-javascript-library.js"></script>
126+
{{ /livewire:assets }}
127+
</body>
128+
```
129+
111130
### Blade or Antlers? Both!
112131
If creating a Livewire component, you need to render a template file
113132

src/Tags/Livewire.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,37 @@ public function scriptConfig(): string
8686
{
8787
return \Livewire\Mechanisms\FrontendAssets\FrontendAssets::scriptConfig();
8888
}
89+
90+
/**
91+
* Antlers implementation of @assets - https://livewire.laravel.com/docs/javascript#loading-assets
92+
*
93+
* {{ livewire:assets }}....{{ /livewire:assets }}
94+
*/
95+
public function assets(): void
96+
{
97+
$html = (string) $this->parse();
98+
99+
$key = md5($html);
100+
101+
if (in_array($key, \Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets::$alreadyRunAssetKeys)) {
102+
// Skip it...
103+
} else {
104+
\Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets::$alreadyRunAssetKeys[] = $key;
105+
\Livewire\store($this->context['__livewire'])->push('assets', $html, $key);
106+
}
107+
}
108+
109+
/**
110+
* Antlers implementation of @script - https://livewire.laravel.com/docs/javascript#executing-scripts
111+
*
112+
* {{ livewire:script }}...{{ /livewire:script }}
113+
*/
114+
public function script(): void
115+
{
116+
$html = trim((string) $this->parse());
117+
118+
$key = md5($html);
119+
120+
\Livewire\store($this->context['__livewire'])->push('scripts', $html, $key);
121+
}
89122
}

0 commit comments

Comments
 (0)