Skip to content

Commit

Permalink
Merge pull request #8 from DigitallyHappy/v2
Browse files Browse the repository at this point in the history
v2
  • Loading branch information
tabacitu authored Nov 24, 2021
2 parents dd2ee94 + 9d6c891 commit e34080e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
39 changes: 23 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,27 @@ $ composer require digitallyhappy/assets

## Usage

Replace your standard CSS and JS loading HTML with the `@asset()` Blade directive this package provides:
Replace your standard CSS and JS loading HTML with the `@loadOnce()` Blade directive this package provides:

```diff
- <script src="{{ asset('path/to/file.js') }}">
+ @asset('path/to/file.js')
+ @loadOnce('path/to/file.js')

- <link href="{{ asset('path/to/file.css') }}" rel="stylesheet" type="text/css">
+ @asset('path/to/file.css')
+ @loadOnce('path/to/file.css')
```

At this moment, the package provides a few Blade directives:
The package provides three Blade directives, in 99% of the cases you'll use `@loadOnce()`:

```php
@asset('path/to/file.css')
@asset('path/to/file.js')
@loadOnce('path/to/file.css')
@loadOnce('path/to/file.js')
// depending of the file extension, the first time it will output
// <link href="{{ asset('path/to/file.css')" rel="stylesheet" type="text/css">
// or
// <script src="{{ asset('path/to/file.js')"></script>
// then the rest of the times this is called... it'll output nothing

// ALTERNATIVELY, if prefer to specify the type of file (CSS/JS) you can call:

@loadCssOnce('path/to/file.css')
// will output <link href="{{ asset('path/to/file.css')"> the first time
// then the second time this is called it'll output nothing

@loadJsOnce('path/to/file.js')
// will output <script src="{{ asset('path/to/file.js')"></script> the first time
// then the second time this is called it'll output nothing

// IN ADDITION, if you have an entire block of HTML that you want to only output once:

@loadOnce('unique_name_for_code_block')
Expand All @@ -65,6 +55,23 @@ At this moment, the package provides a few Blade directives:
// then the second time it will just output nothing
```
However, if you want to pass a _variable_ as the parameter, not a _string_, you'll notice it won't work, because the directive can't tell if it's a CSS, JS or code block. That's why we've created `@loadStyleOnce()` and `@loadScriptOnce()`:
```php
@php
$pathToCssFile = 'path/to/file.css';
$pathToJsFile = 'path/to/file.js';
@endphp
@loadStyleOnce($pathToCssFile)
// will output <link href="{{ asset('path/to/file.css')"> the first time
// then the second time this is called it'll output nothing
@loadScriptOnce($pathToJsFile)
// will output <script src="{{ asset('path/to/file.js')"></script> the first time
// then the second time this is called it'll output nothing
```
## Why does this package exist?
In Laravel 8+, if your CSS or JS assets are loaded inside a blade file:
Expand Down
46 changes: 23 additions & 23 deletions src/blade_directives.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
<?php

Blade::directive('asset', function ($parameter) {
//remove the single/double quotation marks from the parameter to get to the file extension
$extension = trim($parameter, "'");
$extension = trim($extension, '"');
$extension = trim($extension, '`');
$extension = substr($extension, -3);
use Illuminate\Support\Str;

Blade::directive('loadStyleOnce', function ($parameter) {
return "<?php Assets::echoCss({$parameter}); ?>";
});

Blade::directive('loadScriptOnce', function ($parameter) {
return "<?php Assets::echoJs({$parameter}); ?>";
});

Blade::directive('loadOnce', function ($parameter) {
// determine if it's a CSS or JS file
$cleanParameter = Str::of($parameter)->trim("'")->trim('"')->trim('`');
$filePath = Str::of($cleanParameter)->before('?')->before('#');
$extension = substr($filePath, -3);

// mey be useful to get the second parameter
// if (Str::contains($parameter, ',')) {
// $secondParameter = Str::of($parameter)->after(',')->trim(' ');
// }

switch ($extension) {
case 'css':
Expand All @@ -17,27 +31,13 @@
break;

default:
abort(500, 'Could not automatically recognize '.$parameter.' as either a CSS or JS file. Please use @loadCssOnce() or @loadJsOnce() instead of @asset()');
// it's a block start

return "<?php if(! Assets::isAssetLoaded('".$cleanParameter."')) { Assets::markAssetAsLoaded('".$cleanParameter."'); ?>";
break;
}
});

Blade::directive('loadCssOnce', function ($parameter) {
return "<?php Assets::echoCss({$parameter}); ?>";
});

Blade::directive('loadJsOnce', function ($parameter) {
return "<?php Assets::echoJs({$parameter}); ?>";
});

Blade::directive('loadOnce', function ($parameter) {
$parameter = trim($parameter, "'");
$parameter = trim($parameter, '"');
$parameter = trim($parameter, '`');

return "<?php if(! Assets::isAssetLoaded('".$parameter."')) { Assets::markAssetAsLoaded('".$parameter."'); ?>";
});

Blade::directive('endLoadOnce', function () {
return '<?php } ?>';
});

0 comments on commit e34080e

Please sign in to comment.